local extension = Package("rfenghou_jin")
extension.extensionName = "aaa_fenghou"

local U = require "packages/utility/utility"
local RUtil = require "packages/aaa_fenghou/utility/rfenghou_util"
local DIY = require "packages/diy_utility/diy_utility"

Fk:loadTranslationTable{
  ["rfenghou_jin"] = "夺晋",
}

local function AddWinAudio(general)
  local Win = fk.CreateActiveSkill{ name = general.name.."_win_audio" }
  Win.package = extension
  Fk:addSkill(Win)
end

local taoyuanming = General:new(extension, "rfenghou__taoyuanming", "jin", 3)
local funan = fk.CreateTriggerSkill{
  name = "rfenghou__funan",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.HpRecover, fk.CardUseFinished, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and
      not table.contains(player:getTableMark("rfenghou__funan-round"), event) then
      if event == fk.HpRecover then
        return true
      elseif event == fk.CardUseFinished then
        return data.card.sub_type == Card.SubtypeDelayedTrick
      elseif event == fk.TargetConfirmed then
        return data.card.trueName == "analeptic"
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:addTableMark(player, "rfenghou__funan-round", event)
    if player:isKongcheng() then
      player:drawCards(1, self.name)
    else
      player:drawCards(player:getHandcardNum(), self.name)
    end
  end
}
local qianhua = fk.CreateTriggerSkill{
  name = "rfenghou__qianhua",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.Deathed, fk.CardUseFinished, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and
      not table.contains(player:getTableMark("rfenghou__qianhua-round"), event) then
      if event == fk.Deathed then
        return data.damage and data.damage.from and data.damage.from == player
      elseif event == fk.CardUseFinished then
        return target == player and data.card.type == Card.TypeEquip and data.card.sub_type == Card.SubtypeWeapon
      elseif event == fk.TargetConfirmed then
        return  target == player and data.card.trueName == "slash" and data.card.color == Card.Red
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "rfenghou__qianhua-round", event)
    if player:getHandcardNum() > 1 then
      local x = player:getHandcardNum() // 2
      room:askForDiscard(player, x, x, false, self.name, false)
    end
  end
}
taoyuanming:addSkill(funan)
taoyuanming:addSkill(qianhua)
Fk:loadTranslationTable{
  ["rfenghou__taoyuanming"] = "陶渊明",
  ["#rfenghou__taoyuanming"] = "苗疏心旷",
  ["designer:rfenghou__taoyuanming"] = "孤城",
  ["illustrator:rfenghou__taoyuanming"] = "珊瑚虫",

  ["rfenghou__funan"] = "浮南",
  [":rfenghou__funan"] = "锁定技，每轮每项限一次，你于以下时机摸手牌数的牌（至少一张）：回复体力后，使用延时锦囊后，成为【酒】的目标后。",
  ["rfenghou__qianhua"] = "堑华",
  [":rfenghou__qianhua"] = "锁定技，每轮每项限一次，你于以下时机弃置一半手牌（向下取整）：杀死角色后，使用武器牌后，成为红色【杀】的目标后。",
}


--FY012 潘岳夏侯湛 by折翼
local panyuexiahouzhan = General:new(extension, "rfenghou__panyuexiahouzhan", "jin", 3)
local zhizhuan = fk.CreateTriggerSkill{
  name = "rfenghou__zhizhuan",
  switch_skill_name = "rfenghou__zhizhuan",
  anim_type = "switch",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:usedSkillTimes(self.name) == 0
    and (player.phase == Player.Start or player.phase == Player.Finish)
  end,
  on_cost = function(self, event, target, player, data)
    if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
      if player:isKongcheng() or player:getHandcardNum() % 2 == 1 then
        return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__zhizhuan_yang-invoke:::0")
      else
        local n = player:getHandcardNum() // 2
        local cards = player.room:askForDiscard(player, n, n, false, self.name, true, nil,
          "#rfenghou__zhizhuan_yang-invoke:::"..n, true)
        if #cards == n then
          self.cost_data = {cards = cards}
          return true
        end
      end
    else
      return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__zhizhuan_yin-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(self.name, true) == fk.SwitchYang then
      if player:getHandcardNum() % 2 == 1 then
        room:setPlayerMark(player, MarkEnum.SwithSkillPreName..self.name, player:getSwitchSkillState(self.name, true))
        player:setSkillUseHistory(self.name, player:usedSkillTimes(self.name, Player.HistoryGame) - 1, Player.HistoryGame)
        player:drawCards(player:getHandcardNum(), self.name)
      else
        if not player:isKongcheng() then
          room:throwCard(self.cost_data.cards, self.name, player, player)
          if not player.dead and not player:isKongcheng() then
            player:drawCards(player:getHandcardNum(), self.name)
          end
        end
      end
    else
      if not player:isKongcheng() then
        player:drawCards(player:getHandcardNum(), self.name)
      end
      if player:getHandcardNum() % 2 == 0 then
        if not player.dead and not player:isKongcheng() then
          local n = player:getHandcardNum() // 2
          room:askForDiscard(player, n, n, false, self.name, false)
        end
      else
        room:setPlayerMark(player, MarkEnum.SwithSkillPreName..self.name, player:getSwitchSkillState(self.name, true))
        player:setSkillUseHistory(self.name, player:usedSkillTimes(self.name, Player.HistoryGame) - 1, Player.HistoryGame)
      end
    end
  end,
}
local leishi = fk.CreateTriggerSkill{
  name = "rfenghou__leishi",
  events = {fk.BeforeDrawCard},
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("@@rfenghou__leishi") == 0
    and (data.num == 2 or player:isWounded())
  end,
  on_cost = function (self, event, target, player, data)
    if data.num == 2 then
      return true--player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__leishi-change")
    else
      return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__leishi-recover")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name,
    })
    if data.num == 2 then
      return true
    end
  end,
}
panyuexiahouzhan:addSkill(zhizhuan)
panyuexiahouzhan:addSkill(leishi)
Fk:loadTranslationTable{
  ["rfenghou__panyuexiahouzhan"] = "潘岳夏侯湛",
  ["#rfenghou__panyuexiahouzhan"] = "江藻翻沙",
  ["illustrator:rfenghou__panyuexiahouzhan"] = "sier&食茸",
  ["designer:rfenghou__panyuexiahouzhan"] = "折翼",
  ["cv:rfenghou__panyuexiahouzhan"] = "子瞻",

  ["rfenghou__zhizhuan"] = "枝转",
  [":rfenghou__zhizhuan"] = "转换技，每回合限一次，准备阶段或结束阶段，你可以：阳：弃置一半手牌，然后摸手牌数的牌；阴：摸手牌数的牌，"..
  "然后弃置一半手牌。若以此法弃置牌时需取整，则跳过弃置流程，且本次不进行转换。",
  ["#rfenghou__zhizhuan"] = "枝转",
  ["#rfenghou__zhizhuan_yang-invoke"] = "枝转：是否弃置一半手牌（%arg张），然后摸手牌数的牌？",
  ["#rfenghou__zhizhuan_yin-invoke"] = "枝转：是否摸手牌数的牌，然后弃置一半手牌？",

  ["rfenghou__leishi"] = "诔世",
  [":rfenghou__leishi"] = "当你摸牌时，若此次摸牌数：为2，你须放弃此次摸牌，改为回复1点体力；不为2，你可以回复1点体力。",
  ["#rfenghou__leishi-change"] = "诔世：你可以放弃摸两张牌，改为回复1点体力",
  ["#rfenghou__leishi-recover"] = "诔世：你可以回复1点体力",
  --["@@rfenghou__leishi"] = "诔世失效",

  ["$rfenghou__zhizhuan1"] = "听离鸿之晨吟兮，望流火之余景。",
  ["$rfenghou__zhizhuan2"] = "感冬索而春敷兮，嗟夏茂而秋落。",
  ["$rfenghou__leishi1"] = "日往哀深、岁暮思繁，灼灼遐清几曾有？",
  ["$rfenghou__leishi2"] = "列宿皎皎、星稀月明，紫薇晖焕又照我。",
  ["~rfenghou__panyuexiahouzhan"] = "翰林鸟、游川鱼，比目双栖、比目双栖。",
}

local simazhong = General:new(extension, "rfenghou__simazhong", "jin", 3)
local shuaizuo = fk.CreateTriggerSkill{
  name = "rfenghou__shuaizuo",
  anim_type = "control",
  events = {fk.Damage, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(self) then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn_event == nil then return end
      local damage_event = room.logic:getCurrentEvent()
      if not damage_event then return end
      if not data.to.dead and not data.to:isNude() then
        if data.to == player then
          if not table.find(player:getCardIds("he"), function (id)
            return not player:prohibitDiscard(id)
          end) then return end
        end
        if event == fk.Damage then
          return player:getMark("rfenghou__shuaizuo1-turn") == 0
        elseif event == fk.Damaged then
          return player:getMark("rfenghou__shuaizuo2-turn") == 0
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__shuaizuo-invoke::"..data.to.id) then
      self.cost_data = {tos = {data.to.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      room:setPlayerMark(player, "rfenghou__shuaizuo1-turn", 1)
    elseif event == fk.Damaged then
      room:setPlayerMark(player, "rfenghou__shuaizuo2-turn", 1)
    end
    local cards = {}
    if data.to == player then
      cards = room:askForDiscard(player, 2, 2, true, self.name, false, nil, "#rfenghou__shuaizuo-discard::"..player.id)
    else
      local n = math.min(#data.to:getCardIds("he"), 2)
      cards = room:askForCardsChosen(player, data.to, n, n, "he", self.name, "#rfenghou__shuaizuo-discard::"..data.to.id)
      room:throwCard(cards, self.name, data.to, player)
    end
    local suits = {}
    for _, id in ipairs(cards) do
      local suit = Fk:getCardById(id):getSuitString(true)
      if suit ~= "log_nosuit" then
        table.insertIfNeed(suits, suit)
      end
    end
    if #suits == 0 then return end
    for _, p in ipairs(room.alive_players) do
      local mark = p:getTableMark("@rfenghou__shuaizuo-turn")
      for _, suit in ipairs(suits) do
        table.insertIfNeed(mark, suit)
      end
      room:setPlayerMark(p, "@rfenghou__shuaizuo-turn", mark)
    end
  end,
}
local shuaizuo_prohibit = fk.CreateProhibitSkill{
  name = "#rfenghou__shuaizuo_prohibit",
  prohibit_use = function(self, player, card)
    return player:getMark("@rfenghou__shuaizuo-turn") ~= 0 and card and card.suit ~= Card.NoSuit and
      not table.contains(player:getTableMark("@rfenghou__shuaizuo-turn"), card:getSuitString(true))
  end,
}
local airong = fk.CreateTriggerSkill{
  name = "rfenghou__airong",
  events = {fk.AskForCardUse},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      (data.cardName == "jink" or data.pattern and Exppattern:Parse(data.pattern):matchExp("jink"))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local yes = false
    player:drawCards(3, self.name)
    if not player.dead and not player:isKongcheng() and
      table.every(room:getOtherPlayers(player), function (p)
        return player:getHandcardNum() > p:getHandcardNum()
      end) then
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn_event ~= nil then
        local to = turn_event.data[1]
        if to ~= player and not to.dead then
          yes = true
          U.swapHandCards(room, player, player, to, self.name)
        end
      end
    end
    local card = Fk:cloneCard("jink")
    card.skillName = self.name
    if yes and not player:prohibitUse(card) then
      data.result = {
        from = player.id,
        card = card,
      }

      if data.eventData then
        data.result.toCard = data.eventData.toCard
        data.result.responseToEvent = data.eventData.responseToEvent
      end
      return true
    end
    return false
  end,
}
local zaiting = fk.CreateTargetModSkill{
  name = "rfenghou__zaiting$",
  frequency = Skill.Compulsory,
  residue_func = function(self, player, skill, scope)
    if player.kingdom == "jin" and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return #table.filter(Fk:currentRoom().alive_players, function (p)
        return p:hasSkill(self)
      end)
    end
  end,
  bypass_distances = function(self, player, skill, card, to)
    return skill.trueName == "slash_skill" and (player:hasSkill(self) or (player.kingdom == "jin" and to:hasSkill(self)))
  end,
}
shuaizuo:addRelatedSkill(shuaizuo_prohibit)
--airong:addRelatedSkill(airong_trigger)
simazhong:addSkill(shuaizuo)
simazhong:addSkill(airong)
simazhong:addSkill(zaiting)
Fk:loadTranslationTable{
  ["rfenghou__simazhong"] = "司马衷",
  ["#rfenghou__simazhong"] = "荒天罔极",
  ["illustrator:rfenghou__simazhong"] = "食茸",
  ["designer:rfenghou__simazhong"] = "末页",
  ["cv:rfenghou__simazhong"] = "KEVIN",

  ["rfenghou__shuaizuo"] = "衰祚",
  [":rfenghou__shuaizuo"] = "每回合各限一次，当你造成或受到伤害后，你可以弃置受伤角色两张牌，然后本回合所有角色只能使用这些花色的牌。",
  ["rfenghou__airong"] = "哀荣",
  [":rfenghou__airong"] = "当你需使用【闪】时，你可以摸三张牌，然后若你手牌数为全场唯一最多，你与当前回合角色交换手牌，视为使用之。",
  ["rfenghou__zaiting"] = "灾庭",
  [":rfenghou__zaiting"] = "主公技，锁定技，晋势力角色出牌阶段使用【杀】次数上限+1，你使用【杀】或晋势力角色对你使用【杀】无距离限制。",
  ["@rfenghou__shuaizuo-turn"] = "衰祚",
  ["#rfenghou__shuaizuo-invoke"] = "衰祚：是否弃置 %dest 两张牌？本回合所有角色只能使用这些花色的牌",
  ["#rfenghou__shuaizuo-discard"] = "衰祚：弃置 %dest 两张牌",
  ["#rfenghou__airong"] = "哀荣：你可以摸三张牌，若手牌数唯一最多，你与当前回合角色交换手牌并视为使用【闪】",

  ["$rfenghou__shuaizuo1"] = "大祚衰、大祚衰，昼哭夜哭聊自慰。",
  ["$rfenghou__shuaizuo2"] = "大祚荣、大祚荣，本枝百世、本枝万世！",
  ["$rfenghou__airong1"] = "侍中、嵇侍中，丹血寄朕朕负卿……",
  ["$rfenghou__airong2"] = "新衣裾、新衣裾，甚暖、甚善！",
  ["$rfenghou__zaiting1"] = "长乐祥、长乐祥，事关休咎岂由朕？",
  ["$rfenghou__zaiting2"] = "天称大，天称大！生灵板荡？罪在四方！",
  ["~rfenghou__simazhong"] = "啊啊啊啊啊啊啊啊啊啊啊啊啊！",
}


local wanglun = General:new(extension, "rfenghou__wanglun", "jin", 3)
local shangzhou = fk.CreateViewAsSkill{
  name = "rfenghou__shangzhou",
  anim_type = "offensive",
  pattern = "slash",
  prompt = function (self, selected, selected_cards)
    if Self:getMark("rfenghou__shangzhou_get-round") > 0 then
      return "#rfenghou__shangzhou_slash1"
    elseif Self:getMark("rfenghou__shangzhou_lose-round") > 0 or
      #table.filter(Self:getCardIds("he"), function (id)
        return not Self:prohibitDiscard(id)
      end) < 2 then
      return "#rfenghou__shangzhou_slash2"
    else
      return "#rfenghou__shangzhou_slash3"
    end
  end,
  card_filter = function(self, to_select, selected)
    if Self:getMark("rfenghou__shangzhou_get-round") > 0 then
      return #selected < 2 and not Self:prohibitDiscard(to_select)
    else
      return false
    end
  end,
  view_as = function(self, cards)
    if #cards == 1 or (Self:getMark("rfenghou__shangzhou_get-round") > 0 and #cards ~= 2) then return end
    local card = Fk:cloneCard("slash")
    card.skillName = self.name
    if #cards == 2 then
      self.cost_data = cards
    else
      self.cost_data = {}
    end
    return card
  end,
  before_use = function (self, player, use)
    if #self.cost_data > 0 then
      player.room:throwCard(self.cost_data, self.name, player, player)
    else
      player:drawCards(2, self.name)
    end
  end,
  enabled_at_play = function (self, player)
    return player:getMark("rfenghou__shangzhou_get-round") == 0 or
      player:getMark("rfenghou__shangzhou_lose-round") == 0
  end,
  enabled_at_response = function (self, player, response)
    return not response and (player:getMark("rfenghou__shangzhou_get-round") == 0 or
      player:getMark("rfenghou__shangzhou_lose-round") == 0)
  end,
}
local shangzhou_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__shangzhou",
  anim_type = "defensive",
  main_skill = shangzhou,
  events = {fk.PreCardEffect},  --离“需要响应时”最接近的时机
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player.id == data.to and data.card.trueName == "slash" and
      (player:getMark("rfenghou__shangzhou_get-round") == 0 or
      (player:getMark("rfenghou__shangzhou_lose-round") == 0 and #player:getCardIds("he") > 1)) and
      not (data.unoffsetable or data.disresponsive or
      table.contains(data.unoffsetableList or {}, player.id) or
      table.contains(data.disresponsiveList or {}, player.id))
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    self.cost_data = {}
    if player:getMark("rfenghou__shangzhou_get-round") > 0 then
      local cards = room:askForDiscard(player, 2, 2, true, "rfenghou__shangzhou", true, nil,
        "#rfenghou__shangzhou_jink1:::"..data.card:toLogString(), true)
      if #cards > 0 then
        self.cost_data = {cards = cards}
        return true
      end
    elseif player:getMark("rfenghou__shangzhou_lose-round") > 0 or
      #table.filter(player:getCardIds("he"), function (id)
        return not player:prohibitDiscard(id)
      end) < 2 then
      return room:askForSkillInvoke(player, "rfenghou__shangzhou", nil,
        "#rfenghou__shangzhou_jink3:::"..data.card:toLogString())
    else
      local extra_data = {
        num = 2,
        min_num = 0,
        include_equip = true,
        skillName = self.name,
        pattern = ".",
      }
      local success, dat = room:askForUseActiveSkill(player, "discard_skill",
        "#rfenghou__shangzhou_jink2:::"..data.card:toLogString(), true, extra_data, true)
      if success then
        if #dat.cards == 2 then
          self.cost_data = {cards = dat.cards}
        end
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data.cards then
      room:throwCard(self.cost_data.cards, "rfenghou__shangzhou", player, player)
    else
      player:drawCards(2, "rfenghou__shangzhou")
    end
    return true
  end,

  refresh_events = {fk.AfterCardsMove, fk.EventAcquireSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player:hasSkill(self, true) and (player:getMark("rfenghou__shangzhou_get-round") == 0 or
        player:getMark("rfenghou__shangzhou_lose-round") == 0) then
        for _, move in ipairs(data) do
          if move.to == player.id and move.toArea == Card.PlayerHand then
            return true
          end
          if move.from == player.id then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
        end
      end
    else
      return target == player and data == self
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Card.PlayerHand then
          room:setPlayerMark(player, "rfenghou__shangzhou_get-round", 1)
        end
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              room:setPlayerMark(player, "rfenghou__shangzhou_lose-round", 1)
              break
            end
          end
        end
      end
    else
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.to == player.id and move.toArea == Card.PlayerHand then
            room:setPlayerMark(player, "rfenghou__shangzhou_get-round", 1)
            return true
          end
        end
      end, Player.HistoryRound)
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == player.id then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                room:setPlayerMark(player, "rfenghou__shangzhou_lose-round", 1)
                return true
              end
            end
          end
        end
      end, Player.HistoryRound)
    end
  end,
}
local jianzao = fk.CreateTriggerSkill{
  name = "rfenghou__jianzao",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.AfterTurnEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.room:getBanner("RoundCount") % 2 == 1 and not player:insideExtraTurn()
  end,
  on_use = function(self, event, target, player, data)
    player:gainAnExtraTurn(true, self.name)
  end,

  refresh_events = {fk.EventTurnChanging},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and data.to == player and player.room:getBanner("RoundCount") % 2 == 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    data.to = player.next
    room.logic:trigger(fk.EventTurnChanging, room.current, data, true)
  end,
}
shangzhou:addRelatedSkill(shangzhou_trigger)
wanglun:addSkill(shangzhou)
wanglun:addSkill(jianzao)
Fk:loadTranslationTable{
  ["rfenghou__wanglun"] = "王沦",
  ["#rfenghou__wanglun"] = "映昙照影",
  ["illustrator:rfenghou__wanglun"] = "君桓文化",
  ["designer:rfenghou__wanglun"] = "廷玉",

  ["rfenghou__shangzhou"] = "尚周",
  [":rfenghou__shangzhou"] = "当你需使用或响应【杀】时，若你本轮：未获得过手牌，你可以摸两张牌；未失去过手牌，"..
  "你可以弃置两张牌。然后视为你使用或响应之。",
  ["rfenghou__jianzao"] = "简造",
  [":rfenghou__jianzao"] = "锁定技，偶数轮次内，你不执行额定回合；奇数轮次内，你于额定回合后执行一个额外回合。",
  ["@rfenghou__shangzhou"] = "尚周",
  ["#rfenghou__shangzhou_slash1"] = "尚周：你可以弃置两张牌，视为使用【杀】",
  ["#rfenghou__shangzhou_slash2"] = "尚周：你可以弃置两张牌，或直接点“确定”摸两张牌，视为使用【杀】",
  ["#rfenghou__shangzhou_slash3"] = "尚周：你可以摸两张牌，视为使用【杀】",
  ["#rfenghou__shangzhou"] = "尚周",
  ["#rfenghou__shangzhou_jink1"] = "尚周：%arg即将对你生效，你可以弃置两张牌响应之",
  ["#rfenghou__shangzhou_jink2"] = "尚周：%arg即将对你生效，你可以弃置两张牌，或直接点“确定”摸两张牌，响应之",
  ["#rfenghou__shangzhou_jink3"] = "尚周：%arg即将对你生效，你可以摸两张牌，响应之",

  ["$rfenghou__shangzhou1"] = "我视天地为三，其为众妙之门。",
  ["$rfenghou__shangzhou2"] = "昔年孔明有言，宁静方能致远。",
  ["$rfenghou__jianzao1"] = "此生所求者，顺心意尔。",
  ["$rfenghou__jianzao2"] = "羡孔丘知天命之岁，叹吾生之不达。",
  ["~rfenghou__wanglun"] = "人间多锦绣，奈何我云不喜。",
}

local wangxuanfu = General:new(extension, "rfenghou__wangxuanfu", "jin", 4)
--[[local hongdang = fk.CreateTriggerSkill{
  name = "rfenghou__hongdang",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and
      (table.every(player.room.alive_players, function (p)
        return p.hp >= player.hp
      end) or
      table.every(player.room.alive_players, function (p)
        return p.hp <= player.hp
      end))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skill = Fk.skills["tianjie"]
    assert(skill ~= nil, "技能“天劫”未加载至服务器！请联系管理员安装“十周年”包")
    skill.cost_data = {tos = {player.id}}
    skill:use(event, target, player, data)
    if player.dead then return end
    if player.maxHp < 5 then
      room:changeMaxHp(player, 1)
    end
    if player.dead or not player:isWounded() then return end
    local n = 0
    room.logic:getActualDamageEvents(1, function (e)
      local damage = e.data[1]
      if damage.from == player and damage.skillName == "tianjie" then
        n = n + damage.damage
      end
    end, Player.HistoryTurn)
    if n > 0 then
      room:recover{
        who = player,
        num = math.min(2 * n, player.maxHp - player.hp),
        recoverBy = player,
        skillName = self.name,
      }
    end
  end,
}
local yundie = fk.CreateViewAsSkill{
  name = "rfenghou__yundie",
  anim_type = "special",
  pattern = ".",
  prompt = "#rfenghou__yundie",
  interaction = function(self)
    local all_names = U.getAllCardNames("b")
    local names = U.getViewAsCardNames(Self, self.name, all_names, nil, Self:getTableMark("rfenghou__yundie-round"))
    table.insert(all_names, 1, "rfenghou__yundie_orig")
    table.insert(names, 1, "rfenghou__yundie_orig")
    return U.CardNameBox { choices = names, all_choices = all_names }
  end,
  card_filter = function(self, to_select, selected)
    if #selected == 0 and table.contains(Self:getCardIds("h"), to_select) and
      not table.contains(DIY.GetShownCards(Self), to_select) then
      local card = Fk:cloneCard(Fk:getCardById(to_select).name)
      return card.type == Card.TypeBasic or card:isCommonTrick()
    end
  end,
  view_as = function(self, cards)
    if #cards == 0 then return end
    self.cost_data = cards
    local card = Fk:cloneCard(Fk:getCardById(cards[1]).name)
    if self.interaction.data ~= "rfenghou__yundie_orig" then
      card = Fk:cloneCard(self.interaction.data)
    end
    card.skillName = self.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    if self.interaction.data ~= "rfenghou__yundie_orig" then
      room:addTableMark(player, "rfenghou__yundie-round", use.card.trueName)
    end
    room:setCardMark(Fk:getCardById(self.cost_data[1]), "@@rfenghou__yundie-inhand", 1)
    DIY.ShowCards(player, self.cost_data)
  end,
  enabled_at_play = function (self, player)
    return not player:isKongcheng() and #DIY.GetShownCards(player) < player:getHandcardNum()
  end,
  enabled_at_response = function (self, player, response)
    return not response and not player:isKongcheng() and #DIY.GetShownCards(player) < player:getHandcardNum()
  end,
}
local yundie_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__yundie_trigger",

  refresh_events = {fk.DrawNCards, fk.EnterDying},
  can_refresh = function (self, event, target, player, data)
    if target == player then
      if event == fk.DrawNCards then
        return player:hasSkill(yundie)
      elseif event == fk.EnterDying then
        return table.find(player:getCardIds("h"), function (id)
          return Fk:getCardById(id):getMark("@@rfenghou__yundie-inhand") > 0
        end)
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      data.n = player.maxHp
    elseif event == fk.EnterDying then
      for _, id in ipairs(player:getCardIds("h")) do
        room:setCardMark(Fk:getCardById(id), "@@rfenghou__yundie-inhand", 0)
      end
      player:filterHandcards()
    end
  end,
}
local yundie_targetmod = fk.CreateTargetModSkill{
  name = "#rfenghou__yundie_targetmod",
  residue_func = function(self, player, skill, scope)
    if player:hasSkill(yundie) and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return player.maxHp - 1
    end
  end,
}
local yundie_filter = fk.CreateFilterSkill{
  name = "#rfenghou__yundie_filter",
  card_filter = function(self, card, player)
    return card:getMark("@@rfenghou__yundie-inhand") > 0 and table.contains(player.player_cards[Player.Hand], card.id)
  end,
  view_as = function(self, card)
    return Fk:cloneCard("jink", card.suit, card.number)
  end,
}
yundie:addRelatedSkill(yundie_trigger)
yundie:addRelatedSkill(yundie_targetmod)
yundie:addRelatedSkill(yundie_filter)
wangxuanfu:addSkill(hongdang)
wangxuanfu:addSkill(yundie)
wangxuanfu:addRelatedSkill("tianjie")]]--
local hongdang = fk.CreateTriggerSkill{
  name = "rfenghou__hongdang",
  anim_type = "support",
  events = {fk.GameStart, fk.Damaged, fk.AfterCardsMove, fk.HpRecover},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:getMark("@rfenghou__hongdang_shenren") < 99 then
      if event == fk.GameStart then
        return true
      elseif event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from == player.id and move.moveReason == fk.ReasonDiscard then
            return true
          end
        end
      elseif event == fk.Damaged then
        return data.damageType == fk.ThunderDamage
      elseif event == fk.HpRecover then
        return target == player
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = 0
    if event == fk.GameStart then
      num = 20
    elseif event == fk.Damaged then
      num = 5 * data.damage
    elseif event == fk.AfterCardsMove then
      num = 10
    elseif event == fk.HpRecover then
      num = 15 * data.num
    end

    local shenren = player:getMark("@rfenghou__hongdang_shenren")
    num = math.min(99 - shenren, num)
    if num > 0 then
      room:setPlayerMark(player, "@rfenghou__hongdang_shenren", shenren + num)
      if player:getMark("@rfenghou__hongdang_shenren") >= 25 and not player:hasSkill("rfenghou_hongdang__xingbian") then
        room:handleAddLoseSkills(player, "rfenghou_hongdang__xingbian")
      end
      if player:getMark("@rfenghou__hongdang_shenren") >= 50 and not player:hasSkill("rfenghou_hongdang__rulv") then
        room:handleAddLoseSkills(player, "rfenghou_hongdang__rulv")
      end
      if player:getMark("@rfenghou__hongdang_shenren") >= 75 and not player:hasSkill("rfenghou_hongdang__lianpian") then
        room:handleAddLoseSkills(player, "rfenghou_hongdang__lianpian")
      end
      if player:getMark("@rfenghou__hongdang_shenren") >= 99 and not player:hasSkill("rfenghou__yundie") then
        room:handleAddLoseSkills(player, "rfenghou__yundie")
      end
    end
  end,

  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    return target == player and data == self
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventAcquireSkill then
      if player:getMark("@rfenghou__hongdang_shenren") >= 25 and not player:hasSkill("rfenghou_hongdang__xingbian") then
        room:handleAddLoseSkills(player, "rfenghou_hongdang__xingbian")
      end
      if player:getMark("@rfenghou__hongdang_shenren") >= 50 and not player:hasSkill("rfenghou_hongdang__rulv") then
        room:handleAddLoseSkills(player, "rfenghou_hongdang__rulv")
      end
      if player:getMark("@rfenghou__hongdang_shenren") >= 75 and not player:hasSkill("rfenghou_hongdang__lianpian") then
        room:handleAddLoseSkills(player, "rfenghou_hongdang__lianpian")
      end
      if player:getMark("@rfenghou__hongdang_shenren") >= 99 and not player:hasSkill("rfenghou__yundie") then
        room:handleAddLoseSkills(player, "rfenghou__yundie")
      end
    else
      local toLose = {
        "-rfenghou_hongdang__xingbian",
        "-rfenghou_hongdang__rulv",
        "-rfenghou_hongdang__lianpian",
        "-rfenghou__yundie",
      }
      room:handleAddLoseSkills(player, table.concat(toLose, "|"))
    end
  end,
}
local xingbian = fk.CreateTriggerSkill{
  name = "rfenghou_hongdang__xingbian",
  frequency = Skill.Compulsory,
  mute = true,
  events = {fk.EventPhaseChanging, fk.EventPhaseProceeding},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.EventPhaseChanging then
        return data.from == Player.Judge and
          table.find(player.room.alive_players, function (p)
            return p:hasDelayedTrick("lightning")
          end)
      elseif event == fk.EventPhaseProceeding then
        return player.phase == Player.Judge and
          not table.find(player.room.alive_players, function (p)
            return p:hasDelayedTrick("lightning")
          end)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.EventPhaseChanging then
      room:notifySkillInvoked(player, self.name, "special")
      room:addPlayerMark(player, "rfenghou_hongdang__xingbian_count", 1)
      room:endTurn()
      room:delay(200)
    elseif event == fk.EventPhaseProceeding then
      room:notifySkillInvoked(player, self.name, "defensive")
      if player:isWounded() then
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name,
        })
      end
      player._phase_end = true
    end
  end,
}
local xingbian_filter = fk.CreateFilterSkill{
  name = "#rfenghou_hongdang__xingbian_filter",
  card_filter = function(self, to_select, player, isJudgeEvent)
    return isJudgeEvent and table.find(Fk:currentRoom().alive_players, function (p)
      return p:getMark("rfenghou_hongdang__xingbian_count") > 0 and p:getMark("rfenghou_hongdang__xingbian_count") % 20 == 0
    end)
  end,
  view_as = function(self, to_select, player)
    return Fk:cloneCard(to_select.name, Card.Spade, 3)
  end,
}
local xingbian_invalidity = fk.CreateInvaliditySkill {
  name = "#rfenghou_hongdang__xingbian_invalidity",
  invalidity_func = function(self, from, skill)
    return skill:isPlayerSkill(from) and not string.find(skill.name, "rfenghou__xingbian") and
    table.find(Fk:currentRoom().alive_players, function (p)
      return p:hasSkill(xingbian)
    end) and
    table.find(Fk:currentRoom().alive_players, function (p)
      return p:hasDelayedTrick("lightning")
    end)
  end
}
local hongdang__rulv = fk.CreateTriggerSkill {
  name = "rfenghou_hongdang__rulv",
  anim_type = "control",
  events = {fk.AskForRetrial},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.card.suit ~= Card.Spade and
      player:usedSkillTimes(self.name, Player.HistoryTurn) < player:getMark(self.name)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__rulv-invoke::"..target.id..":"..data.reason)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local new_card = Fk:cloneCard(data.card.name, Card.Spade, data.card.number)
    new_card.skillName = self.name
    new_card.id = data.card.id
    data.card = new_card
    room:sendLog{
      type = "#ChangedJudge",
      from = player.id,
      to = { data.who.id },
      arg2 = new_card:toLogString(),
      arg = self.name,
    }
  end,

  refresh_events = {fk.Damaged, fk.EventAcquireSkill},
  can_refresh = function (self, event, target, player, data)
    if target == player then
      if event == fk.Damaged then
        return data.damageType == fk.ThunderDamage and player:getMark(self.name) < 3
      elseif event == fk.EventAcquireSkill then
        return data == self
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      room:addPlayerMark(player, self.name, 1)
    elseif event == fk.EventAcquireSkill then
      local n = math.min(3,
      #room.logic:getActualDamageEvents(3, function (e)
        local damage = e.data[1]
        return damage.to == player and damage.damageType == fk.ThunderDamage
      end, Player.HistoryGame))
      room:setPlayerMark(player, self.name, n)
    end
  end,
}
local lianpian = fk.CreateTriggerSkill{
  name = "rfenghou_hongdang__lianpian",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.type == Card.TypeTrick and data.firstTarget and
      table.find(AimGroup:getAllTargets(data.tos), function(id)
        return id ~= player.id and not player.room:getPlayerById(id).dead
      end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(AimGroup:getAllTargets(data.tos)) do
      if id ~= player.id then
        local p = room:getPlayerById(id)
        if p.dead then
          --continue
        elseif p:getMark("__hidden_general") == 0 and p:getMark("__hidden_deputy") == 0 then
          DIY.EnterHidden(p)
        elseif not p.chained then
          p:setChainState(true)
        else
          p:turnOver()
          if not p.dead and p.chained then
            p:setChainState(false)
          end
          if not p.dead then
            local skill = Fk.skills["hidden_skill&"]
            if p:getMark("__hidden_general") ~= 0 or p:getMark("__hidden_deputy") ~= 0 then
              skill:trigger(fk.TurnStart, p, p, data)
            end
          end
        end
      end
    end
  end,
}
local yundie = fk.CreateActiveSkill{
  name = "rfenghou__yundie",
  anim_type = "control",
  prompt = "#rfenghou__yundie",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:setBanner("@[:]rfenghou__yundie", "rfenghou__yundie_banner")
    local cards = {}
    for _, id in ipairs(room.draw_pile) do
      if table.contains({ "thunder__slash", "lightning" }, Fk:getCardById(id).name) then
        table.insert(cards, id)
      end
    end
    for _, id in ipairs(room.discard_pile) do
      if table.contains({ "thunder__slash", "lightning" }, Fk:getCardById(id).name) then
        table.insert(cards, id)
      end
    end

    for _, p in ipairs(room.alive_players) do
      for _, id in ipairs(p:getCardIds("hej")) do
        if table.contains({ "thunder__slash", "lightning" }, Fk:getCardById(id).name) then
          table.insert(cards, id)
        end
      end
    end
    cards = table.filter(cards, function (id)
      return not table.contains(player:getCardIds("h"), id)
    end)

    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, true, player.id)
    end
  end,
}
local yundie_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__yundie_delay",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return target == player and player.room:getBanner("@[:]rfenghou__yundie") and
      (table.every(player.room.alive_players, function (p)
        return p.hp >= player.hp
      end) or
      table.every(player.room.alive_players, function (p)
        return p.hp <= player.hp
      end))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("rfenghou__yundie")
    local n = math.max(1, #table.filter(player:getCardIds("h"), function(c)
      return Fk:getCardById(c).trueName == "jink"
    end))
    room:damage{
      from = player,
      to = player,
      damage = n,
      damageType = fk.ThunderDamage,
      skillName = "rfenghou__tianjie",
    }
    if player.dead then return end
    --if player.maxHp < 5 then
      room:changeMaxHp(player, 1)
    --end
    --[[if player.dead or not player:isWounded() then return end
    local n = 0
    room.logic:getActualDamageEvents(1, function (e)
      local damage = e.data[1]
      if damage.from == player and damage.skillName == "tianjie" then
        n = n + damage.damage
      end
    end, Player.HistoryTurn)
    if n > 0 then
      room:recover{
        who = player,
        num = math.min(2 * n, player.maxHp - player.hp),
        recoverBy = player,
        skillName = self.name,
      }
    end]]--
  end,
}
yundie:addRelatedSkill(yundie_delay)
xingbian:addRelatedSkill(xingbian_filter)
xingbian:addRelatedSkill(xingbian_invalidity)
hongdang.permanent_skill = true
xingbian.permanent_skill = true
hongdang__rulv.permanent_skill = true
lianpian.permanent_skill = true
yundie.permanent_skill = true
AddWinAudio(wangxuanfu)
wangxuanfu:addSkill(hongdang)
wangxuanfu:addRelatedSkill(xingbian)
wangxuanfu:addRelatedSkill(hongdang__rulv)
wangxuanfu:addRelatedSkill(lianpian)
wangxuanfu:addRelatedSkill(yundie)
Fk:loadTranslationTable{
  ["rfenghou__wangxuanfu"] = "王玄甫",
  ["#rfenghou__wangxuanfu"] = "风霜半扇",
  ["illustrator:rfenghou__wangxuanfu"] = "M酱",
  ["designer:rfenghou__wangxuanfu"] = "祭祀",
  ["cv:rfenghou__wangxuanfu"] = "超绝天",

  --[[["rfenghou__hongdang"] = "鸿荡",
  [":rfenghou__hongdang"] = "锁定技，一名角色回合结束时，若你体力值为全场最大或最小，你进行一次〖天劫〗（对自己发动〖天劫〗），然后你"..
  "加1点体力上限（至多为5）并回复此伤害值两倍的体力。",
  ["rfenghou__yundie"] = "云叠",
  [":rfenghou__yundie"] = "你的额定摸牌数与出【杀】次数等于体力上限。你可以明置一张基本牌或普通锦囊牌，视为使用此牌或"..
  "本轮未以此法使用过的基本牌，然后此牌视为【闪】直到你进入濒死状态。",
  ["#rfenghou__yundie"] = "云叠：你可以明置一张基本或普通锦囊牌，视为使用此牌或任意基本牌",
  ["rfenghou__yundie_orig"] = "使用原牌名",
  ["@@rfenghou__yundie-inhand"] = "云叠",
  ["#rfenghou__yundie_filter"] = "云叠",]]--
  ["rfenghou__hongdang"] = "鸿荡",
  [":rfenghou__hongdang"] = "持恒技，游戏开始时，你获得20点神人值；如下情况时，你获得对应数量的神人值：当一名角色受到1点雷电伤害后——" ..
  "5点；当你弃置牌后——10点；当你回复1点体力后——15点。<br>你根据神人值视为拥有以下技能：25点-〖星变〗；50点-〖如律〗；75点-〖连篇〗；" ..
  "99点-〖云叠〗。你的神人值上限为99。",
  ["rfenghou__yundie"] = "云叠",
  [":rfenghou__yundie"] = "持恒技，限定技，出牌阶段，你可以获得所有雷【杀】和【闪电】，然后令场上获得"..
  "<a href='rfenghou__shenren'>“神人再世”</a>光环。",
  ["rfenghou__shenren"] = "体力值为场上最小或最大的角色回合结束时，其进行一次<a href='rfenghou__tianjie'>“天劫”</a>，然后加1点体力上限。",
  ["rfenghou__tianjie"] = "根据手牌中【闪】的数量，对自己造成雷电伤害",
  ["@rfenghou__hongdang_shenren"] = "神人值",
  ["rfenghou_hongdang__xingbian"] = "星变",
  [":rfenghou_hongdang__xingbian"] = "持恒技，锁定技，若场上没有【闪电】，你的判定阶段改为回复1点体力；否则所有角色除〖星变〗以外的技能失效，"..
  "且于判定阶段结束后结束当前回合。",
  ["#rfenghou_hongdang__xingbian_filter"] = "星变",
  ["rfenghou_hongdang__rulv"] = "如律",
  [":rfenghou_hongdang__rulv"] = "持恒技，每回合限X次，一名角色的判定结果生效前，你可以将判定结果改为♠（X为你受到雷电伤害次数，至多为3）。",
  ["rfenghou_hongdang__lianpian"] = "连篇",
  [":rfenghou_hongdang__lianpian"] = "持恒技，锁定技，当你使用锦囊牌指定其他角色为目标后，其隐匿；若其已隐匿，改为其横置；若其已横置，"..
  "改为其依次翻面、重置、登场。",
  ["#rfenghou__yundie"] = "云叠：获得所有雷【杀】和【闪电】，场上获得“神人再世”光环！",
  ["@[:]rfenghou__yundie"] = "",
  ["rfenghou__yundie_banner"] = "神人再世",
  [":rfenghou__yundie_banner"] = "体力值为场上最小或最大的角色回合结束时进行一次“天劫”（根据手牌中【闪】的数量对自己造成雷电伤害），"..
  "然后加1点体力上限！",
  ["#rfenghou__yundie_delay"] = "神人再世",

  ["$rfenghou__hongdang1"] = "貂蝉元出兜鍪，神人本自凡夫。",
  ["$rfenghou__hongdang2"] = "娟娟隔秋水，濯足洞庭兮望八荒。",
  ["$rfenghou_hongdang__xingbian"] = "剥龙换骨星变易，富贵催人无歇灭。",
  ["$rfenghou_hongdang__rulv"] = "胎光延生，爽灵益禄，幽精绝死，急急如律令。",
  ["$rfenghou_hongdang__lianpian"] = "年光似鸟翩翩过，世事如棋局局新。",
  ["$rfenghou__yundie1"] = "荡荡元始初，混沌气未分。三色无中化，回合霭庆云。",
  ["$rfenghou__yundie2"] = "飘飖八极，与神人俱。思得神药，万岁为期。",
  ["~rfenghou__wangxuanfu"] = "虚空俱含万象，一为无量，无量为一。",
  ["$rfenghou__wangxuanfu_win_audio"] = "吸风饮露，不食五谷，心如渊泉，不偎不爱。",
}

local huanwen = General:new(extension, "rfenghou__huanwen", "jin", 4)

local rfenghou__yuba = fk.CreateTriggerSkill{
  name = "rfenghou__yuba",
  anim_type = "offensive",
  events = {fk.Damage, fk.Damaged},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__yuba-invoke:::"..player:getMark("@rfenghou__yuba")) then
      return true
    else
      player.room:addPlayerMark(player, "@rfenghou__yuba", 1)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:getMark("@rfenghou__yuba")
    local y = x - player:getHandcardNum()
    if y > 0 then
      player:drawCards(y, self.name)
    end
    if player.dead then return end
    if #room:askForDiscard(player, 1, 1, true, self.name, true, ".|"..x, "#rfenghou__yuba-discard:::"..x) == 0 then
      local skills = table.map(table.filter(player.player_skills, function (s)
        return s:isPlayerSkill(player) and s.visible
      end), Util.NameMapper)
      if #skills > 0 then
        local skill = room:askForChoice(player, skills, self.name, "#rfenghou__yuba-lose")
        room:handleAddLoseSkills(player, "-"..skill)
      end
    end
  end,

  on_lose = function (self, player)
    if player:getMark("@rfenghou__yuba") ~= 0 then
      player.room:setPlayerMark(player, "@rfenghou__yuba", 0)
      player.tag[self.name] = player:getMark("@rfenghou__yuba")
    end
  end,
  on_acquire = function (self, player)
    if player.tag[self.name] ~= nil then
      player.room:setPlayerMark(player, "@rfenghou__yuba", player.tag[self.name])
    end
  end,
}
huanwen:addSkill(rfenghou__yuba)

local rfenghou__xingjiang = fk.CreateActiveSkill{
  name = "rfenghou__xingjiang",
  min_card_num = 2,
  target_num = 0,
  prompt = "#rfenghou__xingjiang",
  card_filter = function(self, to_select, selected)
    local c = Fk:getCardById(to_select)
    return (c.type == Card.TypeBasic or c:isCommonTrick()) and table.every(selected, function (id)
      return Fk:getCardById(id).name == c.name
    end)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local name = Fk:getCardById(effect.cards[1]).name
    room:throwCard(effect.cards, self.name, player, player)
    if player.dead then return end
    local skillName = "rfenghou__xingjiang_"..name
    if Fk.skills[skillName] == nil then
      for i = 1, 20 do
        local s = Fk.skills["rfenghou__xingjiang_"..i]
        if s and not player:hasSkill(s, true) then
          skillName = s.name
          room:setPlayerMark(player, s.name, name)
          break
        end
      end
    end
    if Fk.skills[skillName] ~= nil then
      room:handleAddLoseSkills(player, skillName)
    end
  end,
}
huanwen:addSkill(rfenghou__xingjiang)

AddWinAudio(huanwen)
Fk:loadTranslationTable{
  ["rfenghou__huanwen"] = "桓温",
  ["#rfenghou__huanwen"] = "游蛟射主",
  ["illustrator:rfenghou__huanwen"] = "祭祀",
  ["designer:rfenghou__huanwen"] = "玄蝶既白",
  ["cv:rfenghou__huanwen"] = "花落",

  ["rfenghou__yuba"] = "欲罢",
  [":rfenghou__yuba"] = "当你造成或受到伤害后，你可以将手牌摸至X张，弃置一张点数为X的牌或失去一个技能。（X为拒绝发动本技能次数）",
  ["#rfenghou__yuba-invoke"] = "欲罢：你可以将手牌摸至 %arg 张，然后须弃置一张 %arg 点牌，否则失去1个技能",
  ["@rfenghou__yuba"] = "欲罢",
  ["#rfenghou__yuba-discard"] = "欲罢：你需弃置一张 %arg 点牌，否则失去1个技能",
  ["#rfenghou__yuba-lose"] = "欲罢：请选择一个技能并失去",

  ["rfenghou__xingjiang"] = "行将",
  [":rfenghou__xingjiang"] = "出牌阶段限一次，你可以弃置至少两张同名基本或普通锦囊牌，获得一个“每轮一次，你可以视为使用此牌”的技能。（每牌名限获得一个）",
  ["#rfenghou__xingjiang"] = "你可弃置至少两张同名基本牌或普通锦囊牌，转化为每轮限一次的技能",
  ["@$rfenghou__xingjiang"] = "行将",

  ["$rfenghou__yuba1"] = "昔年种柳，今看摇落。树犹如此，人何以堪。",
  ["$rfenghou__yuba2"] = "既不能流芳后世，不足复遗臭万载！",
  ["$rfenghou__xingjiang1"] = "寡人不德，忝荷戎重，经营旧邑，瞻望华夏。",
  ["$rfenghou__xingjiang2"] = "据河洛，襟秦赵，远不五载，大事必定。",
  ["$rfenghou__xingjiang3"] = "任大者思远，思远者忘近。",
  ["$rfenghou__xingjiang4"] = "制五兵之利 ，为万国之资。",
  ["~rfenghou__huanwen"] = "西行北转终回首，如今江左是长安！",
  ["$rfenghou__huanwen_win_audio"] = "天运循环，中国气盛；治世安民，舍我其谁？",
}

local simachi = General:new(extension, "rfenghou__simachi", "jin", 3)

local rfenghou__kuqiu = fk.CreateTriggerSkill{
  name = "rfenghou__kuqiu",
  anim_type = "drawcard",
  attached_skill_name = "rfenghou__kuqiu_other&",
}
local rfenghou__kuqiu_other = fk.CreateActiveSkill{
  name = "rfenghou__kuqiu_other&",
  anim_type = "support",
  prompt = "#rfenghou__kuqiu-active",
  mute = true,
  can_use = function(self, player)
    local targetRecorded = player:getTableMark("rfenghou__kuqiu_other_sources-phase")
    return table.find(Fk:currentRoom().alive_players, function(p)
      return p ~= player and p:hasSkill(rfenghou__kuqiu) and not table.contains(targetRecorded, p.id)
    end)
  end,
  card_num = 1,
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    if #selected == 0 and to_select ~= Self.id and Fk:currentRoom():getPlayerById(to_select):hasSkill(rfenghou__kuqiu) then
      local targetRecorded = Self:getTableMark("rfenghou__kuqiu_other_sources-phase")
      return not table.contains(targetRecorded, to_select)
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:notifySkillInvoked(target, rfenghou__kuqiu.name)
    target:broadcastSkillInvoke(rfenghou__kuqiu.name)
    room:doIndicate(player.id, {target.id})
    room:addTableMark(player, "rfenghou__kuqiu_other_sources-phase", target.id)
    room:moveCardTo(effect.cards, Player.Hand, target, fk.ReasonGive, self.name, nil, true)
    if target.dead then return end
    local card = Fk:cloneCard("amazing_grace")
    card.skillName = "rfenghou__kuqiu"
    if target:prohibitUse(card) or target:isProhibited(target, card) then return end
    local x = math.max(1, target:getLostHp()) - 1
    local targets = {target}
    local avail = table.filter(room:getOtherPlayers(target), function (p) return not target:isProhibited(p, card) end)
    if #avail > 0 and x > 0 then
      local tos = room:askForChoosePlayers(target, table.map(avail, Util.IdMapper), 1, x,
      "#rfenghou__kuqiu-choose", self.name, true)
      if #tos > 0 then
        table.insertTable(targets, table.map(tos, Util.Id2PlayerMapper))
      end
    end
    room:useVirtualCard("amazing_grace", nil, target, targets, "rfenghou__kuqiu")
  end,
}
Fk:addSkill(rfenghou__kuqiu_other)
simachi:addSkill(rfenghou__kuqiu)

local rfenghou__shiji = fk.CreateTriggerSkill{
  name = "rfenghou__shiji",
  events = {fk.TargetSpecified},
  mute = true,
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target and target.phase ~= Player.NotActive and data.firstTarget then
      return table.find(AimGroup:getAllTargets(data.tos), function (pid)
        return pid ~= target.id
      end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    local parentUseEvent = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data[1]
      return use.tos and table.contains(TargetGroup:getRealTargets(use.tos), player.id) and e ~= parentUseEvent
    end, Player.HistoryTurn) > 0 then
      player:broadcastSkillInvoke(self.name, 2)
      room:notifySkillInvoked(player, self.name, "negative")
      data.disresponsive = true
      if parentUseEvent then
        local parentUseData = parentUseEvent.data[1]
        parentUseData.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
      end
    else
      player:broadcastSkillInvoke(self.name, 1)
      room:notifySkillInvoked(player, self.name, "defensive")
      table.insertIfNeed(data.nullifiedTargets, player.id)
      if data.to == player.id and data.card.sub_type == Card.SubtypeDelayedTrick then
        AimGroup:cancelTarget(data, player.id)
      end
    end
  end,
}
simachi:addSkill(rfenghou__shiji)

local rfenghou__bingfa = fk.CreateTriggerSkill{
  name = "rfenghou__bingfa$",
  priority = 0.001,
  mute = true,
  events = {fk.EventAcquireSkill},
  frequency = Skill.Compulsory,
  can_trigger = function (self, event, target, player, data)
    return data == self and target == player and player:hasSkill(self)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name, math.random(2))
    room:setTag("rfenghou__bingfa", true)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p.role ~= "lord" then
        room:setPlayerProperty(p, "role_shown", true)
        room:setPlayerProperty(p, "role", "renegade")
      end
    end
  end,

  refresh_events = {fk.GameFinished},
  can_refresh = function(self, event, target, player, data)
    return player.room:getTag("rfenghou__bingfa")
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setTag("rfenghou__bingfa", nil)
    local winner = data
    if type(winner) ~= "string" or winner == "" then return end
    if string.find(winner, "+") then
      -- 主忠胜利，除了主公全变成忠臣
      local roles = string.split(winner, "+")
      winner = roles[1]
      local subrole = roles[#roles]
      for _, p in ipairs(room.players) do
        if p.role ~= winner then
          room:setPlayerProperty(p, "role", subrole)
        end
      end
    else
      for _, p in ipairs(room.players) do
        room:setPlayerProperty(p, "role", winner)
      end
    end
  end,

  --[[
  -- 内奸获胜时，将所有其他内奸角色改为反贼
  refresh_events = {fk.GameOverJudge},
  can_refresh = function(self, event, target, player, data)
    return target == player and player.rest == 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local alive = table.filter(room.players, function(p)
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)
    if #alive == 1 then
      local winner = alive[1]
      if winner.role == "renegade" then
        for _, p in ipairs(room:getOtherPlayers(winner, false, true)) do
          if p.role == "renegade" then
            room:setPlayerProperty(p, "role", "rebel")
          end
        end
      end
      room:gameOver(winner.role)
    end
  end,
  --]]
}
simachi:addSkill(rfenghou__bingfa)


Fk:loadTranslationTable{
  ["rfenghou__simachi"] = "司马炽",
  ["#rfenghou__simachi"] = "悴舞穷华",
  ["illustrator:rfenghou__simachi"] = "鱼贤",
  ["designer:rfenghou__simachi"] = "黄泉",
  ["cv:rfenghou__simachi"] = "幽蝶化烬",

  ["rfenghou__kuqiu"] = "枯秋",
  [":rfenghou__kuqiu"] = "其他角色的出牌阶段限一次，其可以交给你一张牌，然后你视为对包括你的至多X名角色使用一张【五谷丰登】。（X为你已损失体力，至少为1）",
  ["#rfenghou__kuqiu-active"] = "你可以交给有“枯秋”的角色一张牌，令其使用【五谷丰登】",
  ["rfenghou__kuqiu_other&"] = "枯秋",
  [":rfenghou__kuqiu_other&"] = "出牌阶段限一次，你可以交给有“枯秋”的角色一张牌，令其选择对包括其的至多X名角色使用一张【五谷丰登】（X为其已损失体力，至少为1）",
  ["#rfenghou__kuqiu-choose"] = "枯秋：选择你使用【五谷丰登】的其他目标",

  ["rfenghou__shiji"] = "势羁",
  [":rfenghou__shiji"] = "锁定技，当前回合角色使用牌指定除其外的角色为目标后，若你本回合此前成为过牌的目标，此牌不可以被响应；否则此牌对你无效。",

  ["rfenghou__bingfa"] = "兵伐",
  [":rfenghou__bingfa"] = "主公技，锁定技，非主公外的其他角色的身份改为内奸，且游戏结束时所有角色均胜利。",

  ["$rfenghou__kuqiu1"] = "昔年作词颂春华，今看秋落，翻飞作萧瑟。",
  ["$rfenghou__kuqiu2"] = "往日兴赋宴众宾，此间国破，屈身居檐下。",
  ["$rfenghou__shiji1"] = "朕虽不能登辇拔剑，又岂能坐受此辱！",
  ["$rfenghou__shiji2"] = "手执断笛吟呕哑，难引凤凰来栖...",
  ["$rfenghou__bingfa1"] = "东海王骄肆日久，爱卿奉朕明诏，速讨灭之！",
  ["$rfenghou__bingfa2"] = "石贼乘隙纵难，臣工逡巡，竟使群狼窥伺于朕！",
  ["~rfenghou__simachi"] = "逝将去国兮，逝将去国兮，适幽玄....",
}

local xuxun = General:new(extension, "rfenghou__xuxun", "jin", 2)

local rfenghou__juxie = fk.CreateTriggerSkill{
  name = "rfenghou__juxie",
  events = {fk.CardUseFinished},
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.type == Card.TypeBasic
    and table.every(player.room.alive_players, function (p) return not p.dying end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local _, dat = room:askForUseActiveSkill(player, "rfenghou__juxie_active", "#rfenghou__juxie-ask", true)
    if dat then
      self.cost_data = {choice = dat.interaction, tos = dat.targets, cards = dat.cards}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data.choice == "rfenghou__juxie_draw" then
      room:handleAddLoseSkills(player, "rfenghou__chanyuan")
      player:drawCards(1, self.name)
    else
      local to = room:getPlayerById(self.cost_data.tos[1])
      room:throwCard(self.cost_data.cards, self.name, player, player)
      if not to.dead then
        local num = 3 - to:getHandcardNum()
        if num > 0 then
          to:drawCards(num, self.name)
        end
      end
    end
  end,
}
xuxun:addSkill(rfenghou__juxie)

local rfenghou__juxie_active = fk.CreateActiveSkill{
  name = "rfenghou__juxie_active",
  interaction = function()
    local all_choices = {"rfenghou__juxie_draw", "rfenghou__juxie_throw"}
    local choices = table.simpleClone(all_choices)
    if Self:hasSkill("rfenghou__chanyuan", true) then
      table.remove(choices, 1)
    end
    return UI.ComboBox { choices = choices, all_choices = all_choices }
  end,
  card_filter = function(self, to_select, selected)
    return self.interaction.data == "rfenghou__juxie_throw"
    and #selected == 0 and Fk:getCardById(to_select).suit == Card.Spade
    and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return self.interaction.data == "rfenghou__juxie_throw" and #selected == 0 and #selected_cards == 1
  end,
  feasible = function(self, selected, selected_cards)
    return self.interaction.data ~= "rfenghou__juxie_throw" or (#selected == 1 and #selected_cards == 1)
  end,
}
Fk:addSkill(rfenghou__juxie_active)

local rfenghou__deze = fk.CreateViewAsSkill{
  name = "rfenghou__deze",
  pattern = "slash,peach",
  prompt = "#rfenghou__deze",
  interaction = function(self)
    local names = U.getViewAsCardNames(Self, self.name, {"slash", "peach"})
    if #names > 0 then
      return UI.ComboBox { choices = names }
    end
  end,
  card_filter = Util.FalseFunc,
  before_use = function (self, player, use)
    local room = player.room
    local skills = table.map(table.filter(player.player_skills, function (s)
      return s:isPlayerSkill(player) and s.visible
    end), Util.NameMapper)
    if #skills == 0 then return "" end
    local skill = room:askForChoice(player, skills, self.name, "#rfenghou__deze-delete")
    local cards = player:getCardIds("h")
    player:showCards(cards)
    room:handleAddLoseSkills(player, "-"..skill)
    if table.every(cards, function(id) return Fk:getCardById(id).color == Card.Red end) then
      room:addTableMark(player, "rfenghou__deze-turn", skill)
    end
  end,
  view_as = function(self)
    local name = self.interaction.data
    if name == nil then return end
    local card = Fk:cloneCard(name == "slash" and "thunder__slash" or name)
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return not player:isKongcheng() and table.find(player.player_skills, function (s)
      return s.visible and s:isPlayerSkill(player)
    end)
  end,
  enabled_at_response = function (self, player, response)
    return not response and not player:isKongcheng()
    and table.find(player.player_skills, function (s)
      return s.visible and s:isPlayerSkill(player)
    end)
  end,
}

local rfenghou__deze_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__deze_delay",
  mute = true,
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:getMark("rfenghou__deze-turn") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:handleAddLoseSkills(player, table.concat(player:getMark("rfenghou__deze-turn"), "|"))
  end,
}
rfenghou__deze:addRelatedSkill(rfenghou__deze_delay)

xuxun:addSkill(rfenghou__deze)

local rfenghou__chanyuan = fk.CreateInvaliditySkill {
  name = "rfenghou__chanyuan",
  invalidity_func = function(self, from, skill)
    return from:hasSkill(self, true) and from.hp == 1 and skill:isPlayerSkill(from)
  end
}
xuxun:addRelatedSkill(rfenghou__chanyuan)
AddWinAudio(xuxun)

Fk:loadTranslationTable{
  ["rfenghou__xuxun"] = "许逊",
  ["#rfenghou__xuxun"] = "鄱阳冰心",
  ["illustrator:rfenghou__xuxun"] = "啪啪三国",
  ["designer:rfenghou__xuxun"] = "三秋",
  ["cv:rfenghou__xuxun"] = "超绝天",

  ["rfenghou__juxie"] = "拘邪",
  [":rfenghou__juxie"] = "你使用基本牌结算后，若此时没有角色处于濒死状态，你可以选择一项：1.获得〖缠怨〗，然后摸一张牌；2.弃置一张♠牌并令一名角色将手牌摸至三张。",
  ["rfenghou__juxie_draw"] = "获得“缠怨”并摸1张牌",
  ["rfenghou__juxie_throw"] = "弃置♠牌令一名角色手牌摸至3张",
  ["#rfenghou__juxie-ask"] = "拘邪：你可选一项",
  ["rfenghou__juxie_active"] = "拘邪",

  ["rfenghou__deze"] = "德泽",
  [":rfenghou__deze"] = "你可以展示手牌以将一个技能当雷【杀】或【桃】使用；若均为红色，本回合结束时你复原因此失去的技能。",
  ["#rfenghou__deze"] = "德泽：展示手牌并将一个技能当雷【杀】或【桃】使用",
  ["#rfenghou__deze-delete"] = "德泽：请移去一个技能",
  ["#rfenghou__deze_delay"] = "德泽",

  ["rfenghou__chanyuan"] = "缠怨",
  [":rfenghou__chanyuan"] = "锁定技，若你的体力值为1，你的其他技能失效。",

  ["$rfenghou__juxie1"] = "授汝上清灵章，丹宝飞步，卫道除魔。",
  ["$rfenghou__juxie2"] = "开明灵童，总御火兵，斩邪束妖。",
  ["$rfenghou__deze1"] = "天作云雷，沛然德泽！",
  ["$rfenghou__deze2"] = "三光全、寒暑平，受天祜，享鬼神。",
  ["$rfenghou__xuxun_win_audio"] = "重重道气结成神，玉阙金堂逐日新！",
  ["~rfenghou__xuxun"] = "直饶羽化三清路，终是轮回一幻身。",
}

-- 我靠，这个不写
--[[
local zuti = General:new(extension, "rfenghou__zuti", "jin", 4)
zuti.shield = 3

local rfenghou__tianlin = fk.CreateTriggerSkill{
  name = "rfenghou__tianlin",
  frequency = Skill.Compulsory,
  events = {fk.HpChanged, fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.HpChanged then
        return data.shield_lost and data.shield_lost > 0
      else
        return player.phase ~= Player.NotActive and data.to:getMark("@rfenghou__tianlinShield-turn") ~= 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.HpChanged then
      local damage = data.damageEvent
      if damage and damage.from and not damage.from.dead then
        room:addTableMark(player, "@@rfenghou__tianlinRecord", {damage.from.id, data.shield_lost})
      end
    else
      room:changeShield(player, data.to:getMark("@rfenghou__tianlinShield-turn"))
      room:setPlayerMark(data.to, "@rfenghou__tianlinShield-turn", 0)
    end
  end,

  refresh_events = {fk.TurnEnd, fk.TurnStart},
  can_refresh = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      return target == player and player:getMark("@@rfenghou__tianlinRecord") ~= 0
    else
      return target == player and player:getCurrentExtraTurnReason() == self.name and player:getMark("@@rfenghou__tianlinRecord") ~= 0
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      player:gainAnExtraTurn(true, self.name)
    else
      for _, dat in ipairs(player:getMark("@@rfenghou__tianlinRecord")) do
        room:addPlayerMark(room:getPlayerById(dat[1]), "@rfenghou__tianlinShield-turn", dat[2])
      end
      room:setPlayerMark(player, "@@rfenghou__tianlinRecord", 0)
    end
  end,
}

local rfenghou__tianlin_maxcards = fk.CreateMaxCardsSkill{
  name = "#rfenghou__tianlin_maxcards",
  correct_func = function(self, player)
    local num = 0
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p:hasSkill(rfenghou__tianlin) then
        num = num + p.shield
      end
    end
    return num
  end,
}
rfenghou__tianlin:addRelatedSkill(rfenghou__tianlin_maxcards)

zuti:addSkill(rfenghou__tianlin)

local rfenghou__kuoqing = fk.CreateTriggerSkill{
  name = "rfenghou__kuoqing",
  events = {fk.RoundEnd},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("drowning")
    card.skillName = self.name
    if player:prohibitUse(card) then return false end
    local avilTar = table.map(table.filter(room.alive_players, function (p)
      return not player:isProhibited(p, card)
    end), Util.IdMapper)
    if #avilTar == 0 then return false end
    local num, hp, sheild = 5, 0, 0
    local selected, selectedtar, slots = {}, {}, {}
    local stealTar = table.map(table.filter(room.alive_players, function (p)
      return p ~= player and p:getHandcardNum() == 4
    end), Util.IdMapper)
    while num > 0 do
      local choices = {}


    end
    if num == 0 then
      
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

  end,
}
zuti:addSkill(rfenghou__kuoqing)


Fk:loadTranslationTable{
  ["rfenghou__zuti"] = "祖逖",
  ["#rfenghou__zuti"] = "啼晓见金乌",
  ["illustrator:rfenghou__zuti"] = "",
  ["designer:rfenghou__zuti"] = "秀丽",

  ["rfenghou__tianlin"] = "天霖",
  [":rfenghou__tianlin"] = "锁定技，你的每点护甲为所有角色提供1点手牌上限。你因伤害扣减护甲后，你于当前回合结束后执行一个回合，此回合你首次对伤害来源造成伤害后，恢复对应护甲。",
  ["@rfenghou__tianlinShield-turn"] = "天霖:甲",
  ["@@rfenghou__tianlinRecord"] = "天霖",

  ["rfenghou__kuoqing"] = "廓清",
  [":rfenghou__kuoqing"] = "轮次结束时，你可以将五张牌当【水淹七军】使用；"..
  "<br>你的【杀】或体力可视作两张牌用于转化，然后此牌伤害+1；"..
  "<br>你的护甲或装备栏可视作三张牌用于转化，然后此牌可选目标数+1；"..
  "<br>其他角色仅剩的四张手牌可视作一张牌用于转化，然后此牌目标改为你。",
  ["#rfenghou__kuoqing-ask"] = "廓清：你可将五张牌转化为【水淹七军】（已选：%arg 张）",

  ["rfenghou__kuoqing_slash"] = "【杀】→2张",
  ["rfenghou__kuoqing_hp"] = "体力→2张",
  ["rfenghou__kuoqing_shield"] = "护甲→3张",
  ["rfenghou__kuoqing_slot"] = "装备栏→3张",
  ["rfenghou__kuoqing_other"] = "其他角色手牌→1张",
}
--]]

local simayan = General:new(extension, "rfenghou__simayan", "jin", 3)

--- 查询一名角色区域内可见的杀或锦囊牌
---@param to ServerPlayer @ 目标
---@param from ServerPlayer @ 来源
---@param searchTirck boolean @ 是否搜索锦囊
---@return table
local getXingchuiData = function (to, from, searchTirck)
  local card_data, j_card, h_card = {}, {}, {}
  if searchTirck then
    for _, id in ipairs(to:getCardIds("j")) do
      local c = to:getVirualEquip(id) or Fk:getCardById(id)
      if c.trueName ~= "premeditate" then
        table.insert(j_card, id)
      end
    end
    if #j_card > 0 then
      table.insert(card_data, { "$Judge", j_card })
    end
  end
  for _, id in ipairs(to:getCardIds("h")) do
    if from:cardVisible(id) then
      local c = Fk:getCardById(id)
      if (searchTirck and c.type == Card.TypeTrick) or (not searchTirck and c.trueName == "slash") then
        table.insert(h_card, id)
      end
    end
  end
  if #h_card > 0 then
    table.insert(card_data, { "$Hand", h_card })
  end
  return card_data
end

local rfenghou__xingchui = fk.CreateTriggerSkill {
  name = "rfenghou__xingchui",
  anim_type = "control",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.firstTarget
    and (data.card.trueName == "slash" or data.card.type == Card.TypeTrick)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local searchTirck = data.card.trueName == "slash"
    local targets = table.filter(room.alive_players, function (p)
      return #getXingchuiData(p, player, searchTirck) > 0
    end)
    if #targets == 0 then return false end
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
    searchTirck and "#rfenghou__xingchui-trick" or "#rfenghou__xingchui-slash", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local card_data = getXingchuiData(to, player, data.card.trueName == "slash")
    local card = room:askForCardChosen(player, player, { card_data = card_data }, self.name)
    room:throwCard(card, self.name, to, player)
    if to.dead then return end
    local cards = to:drawCards(2, self.name)
    DIY.ShowCards(to, cards)
    if not player.dead and table.every(cards, function (id)
      return Fk:getCardById(id).trueName ~= "slash" and Fk:getCardById(id).type ~= Card.TypeTrick
    end) then
      RUtil.askForMoveCardFromPlayerArea (to, player, self.name, false)
    end
  end,
}
simayan:addSkill(rfenghou__xingchui)

local rfenghou__yewangs = fk.CreateTriggerSkill {
  name = "rfenghou__yewangs",
  anim_type = "control",
  events = {fk.RoundStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(player)
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    local num = player:getHandcardNum()
    for _, p in ipairs(room:getOtherPlayers(player)) do
      local x = num - p:getHandcardNum()
      if not p.dead and x ~= 0 then
        room:delay(200)
        if x > 0 then
          p:drawCards(1, self.name)
        else
          local cardId = room:askForDiscard(p, 1, 1, false, self.name, false, ".", nil, true)[1]
          if cardId then
            local use, draw
            if p:getHandcardNum() == 1 or table.contains(DIY.GetShownCards(p), cardId) then
              draw = true
              use = U.askForUseRealCard(room, p, {cardId}, ".", self.name, "#rfenghou__yewangs-use", nil, false, false)
            end
            if not use then
              room:throwCard(cardId, self.name, p, p)
            end
            if draw and not player.dead then
              player:drawCards(1, self.name)
            end
          end
        end
      end
    end
  end,
}
simayan:addSkill(rfenghou__yewangs)

local rfenghou__yingxuan = fk.CreateTriggerSkill {
  name = "rfenghou__yingxuan$",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and target.kingdom == "jin"
    and target.phase == Player.Start and not target:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local _, dat = room:askForUseActiveSkill(target, "rfenghou__yingxuan_active", "#rfenghou__yingxuan-ask:"..player.id, true)
    if dat then
      self.cost_data = {tos = {target.id}, cards = dat.cards, choice = dat.interaction}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cardId = self.cost_data.cards[1]
    if self.cost_data.choice == "rfenghou__yingxuan_discard" then
      room:throwCard(cardId, self.name, target, target)
    else
      DIY.HideCards(target, {cardId})
    end
    if player.dead or target.dead then return end
    local names = table.filter(U.getAllCardNames("b"), function (n)
      local card = Fk:cloneCard(n)
      card.skillName = self.name
      return card.trueName ~= Fk:getCardById(cardId).trueName
      and not target:prohibitUse(card) and not  target:isProhibited(player, card)
      and card.skill:modTargetFilter(player.id, {}, target, card, false)
      and (target:canUse(card, {bypass_distances = true, bypass_times = true}) or
      (card.name == "peach" and player:isWounded()))
      -- 耦一个桃
    end)
    if #names > 0 then
      local name = U.askForChooseCardNames(room, target, names, 1, 1, self.name, "#rfenghou__yingxuan-choice:"..player.id)[1]
      room:useVirtualCard(name, nil, target, player, self.name, true)
    end
  end,
}
simayan:addSkill(rfenghou__yingxuan)

local rfenghou__yingxuan_active = fk.CreateActiveSkill{
  name = "rfenghou__yingxuan_active",
  card_num = 1,
  target_num = 0,
  interaction = function()
    local all_choices = {"rfenghou__yingxuan_discard", "rfenghou__yingxuan_hidden"}
    local choices = table.simpleClone(all_choices)
    if #DIY.GetShownCards(Self) then
      table.remove(choices, 2)
    end
    return UI.ComboBox { choices = choices, all_choices = all_choices }
  end,
  card_filter = function(self, to_select, selected)
    if #selected == 0 and Fk:getCardById(to_select).type == Card.TypeBasic then
      if self.interaction.data == "rfenghou__yingxuan_discard" then
        return not Self:prohibitDiscard(Fk:getCardById(to_select))
      else
        return table.contains(DIY.GetShownCards(Self), to_select)
      end
    end
  end,
}
Fk:addSkill(rfenghou__yingxuan_active)

simayan:addSkill("steam__fuyu")

Fk:loadTranslationTable{
  ["rfenghou__simayan"] = "司马炎",
  ["#rfenghou__simayan"] = "紫辰揆序",
  ["illustrator:rfenghou__simayan"] = "鬼画府",
  ["designer:rfenghou__simayan"] = "三秋",

  ["rfenghou__xingchui"] = "星垂",
  [":rfenghou__xingchui"] = "当你使用【杀】/锦囊牌指定目标后，你可以弃置一名角色区域内对你可以见的锦囊牌/【杀】，然后令其摸两张牌并明置之；"..
  "若均不是【杀】或锦囊牌，你移动其区域内一张牌。",
  ["#rfenghou__xingchui-trick"] = "星垂：你可以弃置一名角色区域内对你可以见的锦囊牌",
  ["#rfenghou__xingchui-slash"] = "星垂：你可以弃置一名角色区域内对你可以见的【杀】",

  ["rfenghou__yewangs"] = "烨望",
  [":rfenghou__yewangs"] = "锁定技，轮次开始时，所有角色将手牌数向你调整1；因此弃置了明置牌或最后的手牌的角色改为：使用之并令你摸一张牌。",
  ["#rfenghou__yewangs-use"] = "烨望：你弃置了明置牌或最后的手牌！请使用之",

  ["rfenghou__yingxuan"] = "荧悬",
  [":rfenghou__yingxuan"] = "主公技，其他晋势力角色的准备阶段，其可以暗置或弃置一张基本牌，视为对你使用另一种基本牌。",
  ["#rfenghou__yingxuan-ask"] = "荧悬：你可以暗置或弃置一张基本牌，视为对 %src 使用另一种基本牌",
  ["rfenghou__yingxuan_discard"] = "弃置",
  ["rfenghou__yingxuan_hidden"] = "暗置",
  ["rfenghou__yingxuan_active"] = "荧悬",
  ["#rfenghou__yingxuan-choice"] = "荧悬：选择对 %src 使用的牌",
}

local simawei = General:new(extension, "rfenghou__simawei", "jin", 3)

local rfenghou__mingzhao = fk.CreateTriggerSkill{
  name = "rfenghou__mingzhao",
  anim_type = "control",
  events = {"fk.GeneralAppeared", fk.Death},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(self, false, event == fk.Death)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
    table.removeOne(targets, room.current.id)
    if #targets > 0 and room:askForSkillInvoke(player, self.name, nil, "#rfenghou__mingzhao-invoke:"..room.current.id) then
      self.cost_data = {tos = targets}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(table.map(self.cost_data.tos, Util.Id2PlayerMapper)) do
      if not p.dead and not p:isNude() then
        room:askForDiscard(p, 1, 1, true, self.name, false)
      end
    end
  end,
}
rfenghou__mingzhao.isHiddenSkill = true
simawei:addSkill(rfenghou__mingzhao)

local rfenghou__zhijing = fk.CreateTriggerSkill{
  name = "rfenghou__zhijing",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local numMap = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.to and move.toArea == Card.PlayerHand then
          numMap[move.to] = (numMap[move.to] or 0) + #move.moveInfo
        end
      end
      return false
    end, Player.HistoryRound)
    local maxNum = 0
    for _, num in pairs(numMap) do
      maxNum = math.max(maxNum, num)
    end
    if (numMap[player.id] or 0) == maxNum then
      if data.card.is_damage_card then
        player:broadcastSkillInvoke(self.name, 1)
        room:notifySkillInvoked(player, self.name, "offensive")
        data.additionalDamage = (data.additionalDamage or 0) + 1
      end
    else
      player:broadcastSkillInvoke(self.name, 2)
      room:notifySkillInvoked(player, self.name, "drawcard")
      player:drawCards(1, self.name)
    end
  end,
}
simawei:addSkill(rfenghou__zhijing)

Fk:loadTranslationTable{
  ["rfenghou__simawei"] = "司马玮",
  ["#rfenghou__simawei"] = "终歌的先驱",
  ["illustrator:rfenghou__simawei"] = "",
  ["designer:rfenghou__simawei"] = "空琴",

  ["rfenghou__mingzhao"] = "冥诏",
  [":rfenghou__mingzhao"] = "隐匿，你登场或死亡时，你可以令除当前回合角色外的其他角色各弃置一张牌。",
  ["#rfenghou__mingzhao-invoke"] = "你可以令除 %src 外的其他角色各弃置一张牌",

  ["rfenghou__zhijing"] = "职竞",
  [":rfenghou__zhijing"] = "锁定技，当你使用牌时，若本轮获得牌数最多的角色为你，此牌伤害基数+1，否则你摸一张牌。",

  ["$rfenghou__mingzhao1"] = "今辄奉诏诛贾郭，匡正王室，以安天下！",
  ["$rfenghou__mingzhao2"] = "受诏而行，谓为社稷。",
  ["$rfenghou__zhijing1"] = "众生皆苦，生者当獊囊登阶；遍野横骨，死者为乱世更迭。",
  ["$rfenghou__zhijing2"] = "汝等所为，吾之乘石；吾等所谋，汝之讣告。",
  ["~rfenghou__simawei"] = "受枉如此，望能昭雪。",
}

local zhouchu = General:new(extension, "rfenghou__zhouchu", "jin", 4)

local rfenghou__chuhai = fk.CreateTriggerSkill{
  name = "rfenghou__chuhai",
  events = {fk.CardUsing},
  frequency = Skill.Quest,
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(self) and player:getQuestSkillState(self.name) == nil and data.card.type == Card.TypeBasic
      and player:usedSkillTimes(self.name) == 0 and not player:isNude() then
      local useEvents = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.card.type == Card.TypeBasic
      end, Player.HistoryTurn)
      return #useEvents > 0 and useEvents[1].data[1] ~= data
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choices = {"Top"}
    local current = room.current
    if current and not current.dead and #current:getCardIds("he") > (current == player and 1 or 0) then
      table.insert(choices, current.general)
    end
    if player:getMark("@@rfenghou__chuhai_must") == 0 then
      table.insert(choices, "Cancel")
    end
    local choice = room:askForChoice(player, choices, self.name, "#rfenghou__chuhai-choice")
    if choice == "Cancel" then return false end
    self.cost_data = {tos = {}}
    if choice ~= "Top" then
      self.cost_data.tos = {current.id}
    end
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local wake, to
    if #self.cost_data.tos > 0 then
      to = room.current
    end
    local myNum = to == player and 2 or 1
    local cards = room:askForCard(player, myNum, myNum, true, self.name, false)
    if to then
      if to ~= player then
        table.insert(cards, room:askForCardChosen(player, to, "he", self.name))
      end
    else
      table.insert(cards, room:getNCards(1)[1])
    end
    local handcards = player:getCardIds("h")
    if #handcards > 0 and table.every(handcards, function(id) return table.contains(cards, id) end) then
      wake = true
    end
    player:addToPile("rfenghou__famous", cards, true, self.name)
    if data.toCard then
      data.toCard = nil
    else
      data.nullifiedTargets = table.map(room.players, Util.IdMapper)
    end
    if not player.dead and wake then
      room:notifySkillInvoked(player, self.name, "big")
      room:updateQuestSkillState(player, self.name, false)
      room:handleAddLoseSkills(player, "rfenghou__zhangming")
      if player.dead then return end
      room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
    end
  end,

  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill, fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player:hasSkill(self, true) and player:getMark("@@rfenghou__chuhai_must") ~= 0 then
        for _, move in ipairs(data) do
          if move.from and player.room:getPlayerById(move.from):isKongcheng() and table.find(move.moveInfo, function(info)
            return info.fromArea == Card.PlayerHand
          end) then
            return true
          end
        end
      end
    elseif event == fk.EventAcquireSkill then
      return data == self and target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@rfenghou__chuhai_must", (event == fk.EventAcquireSkill) and 1 or 0)
  end,
}
zhouchu:addSkill(rfenghou__chuhai)

local rfenghou__zhangming = fk.CreateTriggerSkill{
  name = "rfenghou__zhangming",
  events = {fk.CardUseFinished},
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.card.type == Card.TypeBasic and not table.contains(data.card.skillNames, self.name)
    and data.tos and #data.tos > 0 and not (player:getHandcardNum() ~= 1 and #player:getPile("rfenghou__famous") == 0)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(TargetGroup:getRealTargets(data.tos), function(pid) return not room:getPlayerById(pid).dead end)
    local _, dat = room:askForUseActiveSkill(player, "rfenghou__zhangming_active", "#rfenghou__zhangming-use:::"..data.card.name,
    true, {targets = targets, vs_name = data.card.name})
    if dat then
      self.cost_data = {cards = dat.cards, tos = dat.targets}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard(data.card.name)
    card.skillName = self.name
    card:addSubcard(self.cost_data.cards[1])
    room:useCard{ from = player.id, card = card, tos = {self.cost_data.tos}, extraUse = true }
    if not player.dead then
      player:drawCards(2, self.name)
    end
  end,
}
zhouchu:addRelatedSkill(rfenghou__zhangming)

local rfenghou__zhangming_active = fk.CreateActiveSkill{
  name = "rfenghou__zhangming_active",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  expand_pile = "rfenghou__famous",
  card_filter = function(self, to_select, selected)
    if #selected == 0 then
      return Self:getPileNameOfId(to_select) == "rfenghou__famous" or
      (table.contains(Self.player_cards[Player.Hand], to_select) and Self:getHandcardNum() == 1)
    end
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected == 0 and table.contains(self.targets or {}, to_select) and #selected_cards == 1 then
      local name = self.vs_name
      if not name then return false end
      local card = Fk:cloneCard(name)
      card:addSubcard(to_select)
      card.skillName = "rfenghou__zhangming"
      local to = Fk:currentRoom():getPlayerById(to_select)
      return not Self:prohibitUse(card) and not Self:isProhibited(to, card)
      and (card.skill:modTargetFilter(to_select, {}, Self, card, false) or
      (card.name == "peach" and to:isWounded()))
    end
  end,
}
Fk:addSkill(rfenghou__zhangming_active)

Fk:loadTranslationTable{
  ["rfenghou__zhouchu"] = "周处",
  ["#rfenghou__zhouchu"] = "英情天逸",
  ["illustrator:rfenghou__zhouchu"] = "枭瞳",
  ["designer:rfenghou__zhouchu"] = "三秋",

  ["rfenghou__chuhai"] = "除害",
  [":rfenghou__chuhai"] = "使命技，每回合限一次，每回合非首次有角色使用基本牌时，你可以选择一张牌，再选择牌堆顶或当前回合角色的一张牌置为“英”，以令此牌无效；若没有角色失去过最后的手牌，此技能必须发动。"..
  "<br><b>成功：</b>你因此失去最后的手牌后，获得“彰名”并回复1点体力。",
  ["@@rfenghou__chuhai_must"] = "除害:必发",
  ["#rfenghou__chuhai-choice"] = "除害：你可以将你一张牌和（牌堆顶或当前回合角色）的一张牌置为“英”",
  ["rfenghou__famous"] = "英",

  ["rfenghou__zhangming"] = "彰名",
  [":rfenghou__zhangming"] = "一名角色非以此法使用基本牌结算后，你可以将最后的手牌或一张“英”当同名牌对一个目标使用，然后摸两张牌。",
  ["#rfenghou__zhangming-use"] = "彰名：你可以将最后的手牌或一张“英”当【%arg】对同一目标使用",
  ["rfenghou__zhangming_active"] = "彰名",

  ["$rfenghou__chuhai1"] = "斩山上虎，除渊中蛟，灭世间贼！",
  ["$rfenghou__chuhai2"] = "恶疾难愈，唯仗剑除之。",
  ["$rfenghou__zhangming1"] = "当为天下大事，以惊天下之人！",
  ["$rfenghou__zhangming2"] = "三害皆除，万事可成！",

  ["~rfenghou__zhouchu"] = "周处",
}

local ruanxian = General:new(extension, "rfenghou__ruanxian", "jin", 3)

local rfenghou__zhenying = fk.CreateTriggerSkill{
  name = "rfenghou__zhenying",
  events = { fk.EventPhaseEnd },
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and not target.dead and target.phase == Player.Play then
      local suits, currentNum = {}, 0
      local useEvents = player.room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
        local use = e.data[1]
        if use.from == target.id then
          if use.card.suit ~= Card.NoSuit then
            table.insertIfNeed(suits, use.card.suit)
          end
          if use.card.number > 0 then
            if use.card.number > currentNum then
              currentNum = use.card.number
            else
              currentNum = 999
            end
          end
          return true
        end
      end, Player.HistoryTurn)
      return (#useEvents >= 2 and #suits == 1) or (#useEvents >= 3 and currentNum < 999 and currentNum > 0)
    end
  end,
  on_cost = function (self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return player.room:askForSkillInvoke(target, self.name, nil, "#rfenghou__zhenying-invoke:"..player.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = target:drawCards(1, self.name)
    if not target.dead then
      target:showCards(ids)
    end
    if not player.dead then
      local ids2 = player:drawCards(1, self.name)
      table.insert(ids, ids2[1])
      if not player.dead then
        player:showCards(ids2)
      end
    end
    if #ids == 2 and Fk:getCardById(ids[1]).color ~= Fk:getCardById(ids[2]).color and not player.dead
    and room:askForSkillInvoke(player, self.name, nil, "#rfenghou__zhenying-throw") then
      if table.contains(target:getCardIds("h"), ids[1]) then
        room:throwCard(ids[1], self.name, target, player)
      end
      if table.contains(player:getCardIds("h"), ids[2]) then
        room:throwCard(ids[2], self.name, player, player)
      end
    end
  end,
}
ruanxian:addSkill(rfenghou__zhenying)

local rfenghou__erde = fk.CreateTriggerSkill{
  name = "rfenghou__erde",
  anim_type = "drawcard",
  events = { fk.AfterCardsMove },
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      local suits = {}
      for _, move in ipairs(data) do
        if move.from == player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand then
              local card = Fk:getCardById(info.cardId)
              if card.suit ~= Card.NoSuit then
                table.insertIfNeed(suits, Fk:getCardById(info.cardId):getSuitString())
              end
            end
          end
        end
      end
      if #suits > 0 then
        self.cost_data = suits
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local suits = table.simpleClone(self.cost_data)
    local cardMap, choices = {}, {}
    -- 不允许空发，弃牌堆至少有一张对应花色
    for _, suit in ipairs(suits) do
      local get = room:getCardsFromPileByRule(".|.|"..suit, 1, "discardPile")[1]
      if get then
        cardMap[suit] = get
        table.insert(choices, "log_"..suit)
      end
    end
    if #choices == 0 then return false end
    choices = room:askForChoices(player, choices, 1, 1, self.name, "#rfenghou__erde-choice", true)
    if #choices > 0 then
      self.cost_data = cardMap[choices[1]:sub(5, -1)]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      local get = self.cost_data
      room:setPlayerMark(player, "rfenghou__erde-turn", get)
      room:moveCardTo(get, Player.Hand, player, fk.ReasonJustMove, self.name)
  end,
}
ruanxian:addSkill(rfenghou__erde)

Fk:loadTranslationTable{
  ["rfenghou__ruanxian"] = "阮咸",
  ["#rfenghou__ruanxian"] = "铮然希声",
  ["illustrator:rfenghou__ruanxian"] = "率土之滨",
  ["designer:rfenghou__ruanxian"] = "三秋",

  ["rfenghou__zhenying"] = "振萦",
  [":rfenghou__zhenying"] = "一名角色的出牌阶段结束时，若其本阶段使用的牌满足任何一项整肃条件，其可以与你各摸一张牌并展示；若颜色不同，你可以弃置所有展示牌。（擂进：至少使用3张且点数递增；变阵：至少使用2张牌且花色相同）",
  ["#rfenghou__zhenying-invoke"] = "振萦：你可以与 %src 各摸一张牌",
  ["#rfenghou__zhenying-throw"] = "振萦：你可以弃置你与其的展示牌！",

  ["rfenghou__erde"] = "贰德",
  [":rfenghou__erde"] = "每回合限一次，当你弃置手牌后，可以从弃牌堆获得一张同花色的牌。",
  ["#rfenghou__erde-choice"] = "贰德：你可以选择获得一张你弃置花色的牌",
}

local limi = General:new(extension, "rfenghou__limi", "jin", 3)

local addCunxinRecord = function(player, skill)
  if not table.contains(player:getTableMark("rfenghou__cunxin_used"), skill) then
    player.room:addTableMark(player, "rfenghou__cunxin_used", skill)
  end
  if #player:getTableMark("rfenghou__cunxin_used") == 3 then
    player.room:addTableMark(player, "rfenghou__cunxin_removed", skill)
    if player:hasSkill("rfenghou__jiecao", true) then
      local map = {["chenqing"] = "log_heart", ["kanji"] = "log_diamond", ["bingqing"] = "log_spade"}
      player.room:addTableMark(player, "@rfenghou__jiecao", map[skill])
    end
  end
end

local cunxin = fk.CreateActiveSkill{
  name = "rfenghou__cunxin",
  mute = true,
  card_num = 1,
  target_num = 0,
  prompt = "#rfenghou__cunxin-kanji",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).suit == Card.Diamond
    and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  can_use = function(self, player)
    return not table.contains(player:getTableMark("rfenghou__cunxin_removed"), "kanji")
    --and player:usedSkillTimes(self.name, Player.HistoryPhase) < 2
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    addCunxinRecord(player, "kanji")
    player:broadcastSkillInvoke(self.name, 1)
    room:notifySkillInvoked(player, self.name, "drawcard")
    room:throwCard(effect.cards, self.name, player, player)
    if player.dead then return end
    local hancards = player:getCardIds("h")
    player:showCards(hancards)
    local suits, check = {}, true
    for _, id in ipairs(player:getCardIds("h")) do
      if not table.insertIfNeed(suits, Fk:getCardById(id).suit) then
        check = false
      end
    end
    if not check then return end
    -- 祭祀巧思，防止循环扭
    -- 由于NG智睿，此部分作废
    --[[
    if player:usedSkillTimes(self.name, Player.HistoryPhase) > 4 then
      local put, n = {}, 0
      for i = 1, #room.draw_pile do
        local card = Fk:getCardById(room.draw_pile[i])
        if card.suit == Card.Diamond then
          table.insert(put, table.remove(room.draw_pile, i))
        else
          n = n + 1
          if n == 2 then break end
        end
      end
      for _, id in ipairs(put) do
        table.insert(room.draw_pile, id)
      end
    end
    --]]

    player:drawCards(2, self.name)
    if player.dead or #suits == 4 then return end
    suits = {}
    for _, id in ipairs(player:getCardIds("h")) do
      if table.insertIfNeed(suits, Fk:getCardById(id).suit) and #suits == 4 then break end
    end
    if #suits == 4 then
      player:skip(Player.Discard)
    end
  end,
}

local cunxin_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__cunxin_trigger",
  mute = true,
  events = {fk.EnterDying, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player:isNude() then return false end
    if event == fk.EnterDying then
      return not table.contains(player:getTableMark("rfenghou__cunxin_removed"), "chenqing")
    else
      return not table.contains(player:getTableMark("rfenghou__cunxin_removed"), "bingqing")
      and data.extra_data and data.extra_data.rfenghou__cunxin_BingqingCount
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets, prompt = {}, ""
    local ids = table.filter(player:getCardIds("he"), function (id)
      if not player:prohibitDiscard(id) then
        return Fk:getCardById(id).suit == (event == fk.EnterDying and Card.Heart or Card.Spade)
      end
    end)
    if event == fk.EnterDying then
      prompt = "#rfenghou__cunxin-chenqing:"..target.id
      targets = table.filter(room.alive_players, function (p)
        return p ~= target and p ~= player
      end)
    else
      local count = data.extra_data.rfenghou__cunxin_BingqingCount
      prompt = "#rfenghou__cunxin-bingqing"..count
      if count == 2 then
        targets = room.alive_players
      elseif count == 3 then
        targets = table.filter(room.alive_players, function (p) return not p:isAllNude() end)
      else
        targets = room:getOtherPlayers(player, false)
      end
    end
    if #targets == 0 then return end
    targets, ids = room:askForChooseCardsAndPlayers(player, 1, 1, table.map(targets, Util.IdMapper), 1, 1,
    tostring(Exppattern{ id = ids }), prompt, self.name, true)
    if #ids > 0 then
      self.cost_data = {tos = targets, cards = ids}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skillName = cunxin.name
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:throwCard(self.cost_data.cards, skillName, player, player)
    if event == fk.EnterDying then
      addCunxinRecord(player, "chenqing")
      player:broadcastSkillInvoke(skillName, 2)
      room:notifySkillInvoked(player, skillName, "support", {target.id, to.id})
      if to.dead then return end
      to:drawCards(4, skillName)
      if to.dead then return end
      local cards = room:askForDiscard(to, 4, 4, true, skillName, false, ".",
      "#rfenghou__cunxin_chenqing-discard:"..target.id, true)
      local suits = {}
      for _, id in ipairs(cards) do
        if Fk:getCardById(id).suit ~= Card.NoSuit then
          table.insertIfNeed(suits, Fk:getCardById(id).suit)
        end
      end
      room:throwCard(cards, skillName, to, to)
      if #suits == 4 and not target.dead then
        room:useVirtualCard("peach", nil, to, target, skillName)
      end
    else
      addCunxinRecord(player, "bingqing")
      player:broadcastSkillInvoke(skillName, 3)
      local count = data.extra_data.rfenghou__cunxin_BingqingCount
      if count == 2 then
        room:notifySkillInvoked(player, skillName, "support", {to.id})
        to:drawCards(2, skillName)
      elseif count == 3 then
        room:notifySkillInvoked(player, skillName, "control", {to.id})
        if not player.dead and not to:isAllNude() then
          local cid = room:askForCardChosen(player, to, "hej", self.name)
          room:throwCard(cid, skillName, to, player)
        end
      else
        room:notifySkillInvoked(player, skillName, "offensive", {to.id})
        room:damage { from = player, to = to, damage = 1, skillName = self.name }
      end
    end
  end,

  refresh_events = {fk.CardUsing},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self, true) and player.phase == Player.Play and data.card.suit ~= Card.NoSuit
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local check, suits = true, {}
    local useEvents = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data[1]
      if use.from == player.id and use.card.suit ~= Card.NoSuit then
        if use.card.suit == data.card.suit and data ~= use then
          check = false
          return true
        end
        table.insertIfNeed(suits, use.card.suit)
      end
      return false
    end, Player.HistoryPhase)
    if check and #suits > 1 then
      data.extra_data = data.extra_data or {}
      data.extra_data.rfenghou__cunxin_BingqingCount = #suits
    end
  end,
}
cunxin:addRelatedSkill(cunxin_trigger)
limi:addSkill(cunxin)

local jiecao = fk.CreateTriggerSkill{
  name = "rfenghou__jiecao",
  anim_type = "support",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and
    target ~= player and not player:isKongcheng() and table.every(player:getCardIds("h"), function (id)
      return table.contains(player:getTableMark("@rfenghou__jiecao"), Fk:getCardById(id):getSuitString(true))
    end)
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__jiecao-invoke:"..target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recastCard(player:getCardIds("h"), player, self.name)
    if not target.dead then
      room:recover { num = 1, skillName = self.name, who = target, recoverBy = player }
    end
  end,

  on_acquire = function (self, player)
    player.room:setPlayerMark(player, "@rfenghou__jiecao", {"log_club"})
  end,
  on_lose = function (self, player)
    player.room:setPlayerMark(player, "@rfenghou__jiecao", 0)
  end,
}
limi:addSkill(jiecao)

-- 注释用空壳技能
local kanji = fk.CreateActiveSkill{
  name = "rfenghou__kanji",
}
limi:addRelatedSkill(kanji)

local chenqing = fk.CreateTriggerSkill{
  name = "rfenghou__chenqing",
}
limi:addRelatedSkill(chenqing)

local bingqing = fk.CreateTriggerSkill{
  name = "rfenghou__bingqing",
}
limi:addRelatedSkill(bingqing)

Fk:loadTranslationTable{
  ["rfenghou__limi"] = "李密",
  ["#rfenghou__limi"] = "情切哺乌",
  ["illustrator:rfenghou__limi"] = "佚名",
  ["designer:rfenghou__limi"] = "空琴",

  ["rfenghou__cunxin"] = "寸心",
  [":rfenghou__cunxin"] = "你可以弃置一张<font color='red'>♥/♦</font>/♠牌并发动“陈情”/“勘集”/“秉清”。若已触发该技能所有效果，删除当前项并令对应花色加入“结草”。",
  ["#rfenghou__cunxin_trigger"] = "寸心",
  ["#rfenghou__cunxin-chenqing"] = "寸心：你可弃置一张<font color='red'>♥</font>牌令一名其他角色摸4弃4，若弃4色则 %src 回复1点体力",
  ["#rfenghou__cunxin-kanji"] = "勘集：弃置1张<font color='red'♦</font>牌再展示手牌，若花色均不同，摸2张牌，若因此含4色，跳过弃牌",
  ["#rfenghou__cunxin_chenqing-discard"] = "寸心(陈情)：请弃置四张牌，若弃置了四种花色，视为对 %src 使用【桃】",
  ["#rfenghou__cunxin-bingqing2"] = "寸心：你可弃♠牌发动“秉清”：令一名角色摸两张牌",
  ["#rfenghou__cunxin-bingqing3"] = "寸心：你可弃♠牌发动“秉清”：弃置一名角色区域里的一张牌",
  ["#rfenghou__cunxin-bingqing4"] = "寸心：你可弃♠牌发动“秉清”：对一名其他角色造成1点伤害",

  ["rfenghou__chenqing"] = "陈情",
  [":rfenghou__chenqing"] = "当一名角色进入濒死状态时，你可以令另一名其他角色摸四张牌，然后弃置四张牌，若其以此法弃置的四张牌的花色各不相同，则其视为对濒死状态的角色使用一张【桃】。",
  ["rfenghou__kanji"] = "勘集",
  [":rfenghou__kanji"] = "出牌阶段，你可以展示所有手牌，若花色均不同，你摸两张牌，然后若因此使手牌包含四种花色，则你跳过本回合的弃牌阶段。",
  ["rfenghou__bingqing"] = "秉清",
  [":rfenghou__bingqing"] = "你于出牌阶段内使用每花色首张牌结算后，根据你本阶段使用花色数，可以执行对应效果：两种，令一名角色摸两张牌；三种，弃置一名角色区域内的一张牌；四种，对一名角色造成1点伤害。",

  ["rfenghou__jiecao"] = "结草",
  [":rfenghou__jiecao"] = "每回合限一次，当其他角色进入濒死状态时，若你手牌中仅有♣花色，你可以重铸所有手牌并令其回复1点体力。",
  ["@rfenghou__jiecao"] = "结草",
  ["#rfenghou__jiecao-invoke"] = "结草：你可以重铸所有手牌，令 %src 回复1点体力",

  ["$rfenghou__cunxin1"] = "", -- 勘集
  ["$rfenghou__cunxin2"] = "", -- 陈情
  ["$rfenghou__cunxin3"] = "", -- 秉清
}

local liangshanbozhuyingtai = General:new(extension, "rfenghou__liangshanbozhuyingtai", "jin", 3, 3, General.Bigender)
local hedui = fk.CreateActiveSkill{
  name = "rfenghou__hedui",
  anim_type = "special",
  card_num = 2,
  prompt = function (self, selected_cards, selected_targets)
    if #selected_cards == 0 then
      return "#rfenghou__hedui0"
    elseif #selected_cards == 1 then
      local card = Fk:getCardById(selected_cards[1])
      local color = card.color == Card.Black and "red" or "black"
      return "#rfenghou__hedui1:::"..Fk:translate(card.trueName, "zh_CN"):len()..":"..color
    elseif #selected_cards == 2 then
      local card1 = Fk:getCardById(selected_cards[1])
      local card2 = Fk:getCardById(selected_cards[2])
      return "#rfenghou__hedui2:::"..card1.trueName..":"..card2.trueName
    end
  end,
  can_use = function(self, player)
    return true--player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    if #selected < 2 and table.contains(Self:getCardIds("h"), to_select) and
      (card.type == Card.TypeBasic or card:isCommonTrick()) and card.color ~= Card.NoColor and
      not Self:prohibitDiscard(Fk:getCardById(to_select)) then
      if #selected == 0 then
        return true
      elseif #selected == 1 then
        local card1 = Fk:getCardById(selected[1])
        return Fk:translate(card.trueName, "zh_CN"):len() == Fk:translate(card1.trueName, "zh_CN"):len() and
          card:compareColorWith(card1, true)
      end
    end
  end,
  target_filter = function(self, to_select, selected, selected_cards, _, _, player)
    if #selected_cards == 2 then
      local card = Fk:getCardById(selected_cards[2])
      if card.is_passive or card.skill:getMinTargetNum() == 0 then
        return false
      else
        return card.skill:targetFilter(to_select, {}, {}, card, {bypass_times = true}, player)
      end
    end
  end,
  feasible = function (self, selected, selected_cards, player)
    if #selected_cards == 2 then
      local card = Fk:getCardById(selected_cards[2])
      if card.is_passive or card.skill:getMinTargetNum() == 0 then
        return #selected == 0
      else
        local targets = table.filter(Fk:currentRoom().alive_players, function (p)
          return card.skill:targetFilter(p.id, {}, {}, card, {bypass_times = true}, player)
        end)
        if #targets > 0 then
          return #selected <= card.skill:getMaxTargetNum(Self, card) and #selected >= card.skill:getMinTargetNum()
        else
          return #selected == 0
        end
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:throwCard(effect.cards, self.name, player, player)
    local targets = effect.tos
    local card2 = Fk:getCardById(effect.cards[2])
    if card2.trueName == "collateral" and #effect.tos > 0 then  --第一锦囊的含金量
      targets = {effect.tos[1]}
    end
    if #targets == 0 then
      if card2.skill:getMinTargetNum() == 0 then
        if card2.is_passive or card2.trueName == "collateral" then
          --
        elseif card2.multiple_targets then
          local use = {
            from = player.id,
            card = Fk:cloneCard(card2.name),
            tos = {},
          }
          card2.skill:onUse(room, use)
          targets = table.simpleClone(TargetGroup:getRealTargets(use.tos))
        else
          if player:canUse(card2, {bypass_times = true}) then
            targets = {player.id}
          end
        end
      end
      if #targets == 0 then
        if #room.alive_players == 0 then
          return
        else
          targets = table.map(room:getAlivePlayers(), Util.IdMapper)
        end
      end
    end
    local card = Fk:cloneCard(Fk:getCardById(effect.cards[1]).name)
    card.skillName = self.name
    room:sortPlayersByAction(targets)
    room:useCard{
      from = player.id,
      tos = table.map(targets, function (id)
        return {id}
      end),
      card = card,
      extraUse = true,
    }
  end,
}
local hedui_skill = fk.CreateActiveSkill{
  name = "rfenghou__hedui_skill",
  target_num = 1,
  mod_target_filter = Util.TrueFunc,
  target_filter = Util.TrueFunc,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if not to.dead and not to:isNude() then
      local card = room:askForCard(to, 1, 1, true, self.name, false, nil, "#rfenghou__hedui-recast")
      room:recastCard(card, to, self.name)
    end
  end,
}
hedui_skill.cardSkill = true
Fk:addSkill(hedui_skill)
local hedui_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__hedui_trigger",

  refresh_events = {fk.PreCardEffect},
  can_refresh = function(self, event, target, player, data)
    return target == player and table.contains(data.card.skillNames, "rfenghou__hedui") and data.to and
      not data.card.skill:modTargetFilter(data.to, {}, player, data.card, false)
  end,
  on_refresh = function (self, event, target, player, data)
    local card = data.card:clone()
    local c = table.simpleClone(data.card)
    for k, v in pairs(c) do
      card[k] = v
    end
    card.skill = hedui_skill
    data.card = card
  end,
}
local huadie = fk.CreateTriggerSkill{
  name = "rfenghou__huadie",
  anim_type = "support",
  frequency = Skill.Wake,
  events = {fk.AskForPeaches},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_wake = function (self, event, target, player, data)
    return player.dying
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover({
      who = player,
      num = player.maxHp - player.hp,
      recoverBy = player,
      skillName = self.name,
    })
    if player.dead then return end
    if player:getHandcardNum() < player.maxHp then
      player:drawCards(player.maxHp - player:getHandcardNum(), self.name)
    end
    if player.dead then return end
    local generals = {{}, {}}
    for name, general in pairs(Fk.generals) do
      if Util.TranslateMapper("designer:"..name) == "玄蝶既白" and
        table.find(general:getSkillNameList(true), function (skill)
          return not player:hasSkill(skill, true)
        end) then
        if general.gender == General.Male then
          table.insert(generals[1], general.name)
        elseif general.gender == General.Female then
          table.insert(generals[2], general.name)
        end
      end
    end
    for i = 1, 2, 1 do
      if #generals[i] > 0 then
        local g = table.random(generals[i], 3)
        local skills = table.map(g, function (name)
          return table.filter(Fk.generals[name]:getSkillNameList(true), function (skill)
            return not player:hasSkill(skill, true)
          end)
        end)
        local result = room:askForCustomDialog(player, self.name,
        "packages/aaa_fenghou/qml/ChooseGeneralSkillsBox.qml", {
          g, skills, 1, 1, "#rfenghou__huadie-choice", false
        })
        if result ~= "" then
          skills = json.decode(result)
        end
        room:handleAddLoseSkills(player, skills, nil)
      end
    end
  end,
}
hedui:addRelatedSkill(hedui_trigger)
liangshanbozhuyingtai:addSkill(hedui)
liangshanbozhuyingtai:addSkill(huadie)
Fk:loadTranslationTable{
  ["rfenghou__liangshanbozhuyingtai"] = "梁山伯祝英台",
  ["#rfenghou__liangshanbozhuyingtai"] = "共冢同梦",
  ["illustrator:rfenghou__liangshanbozhuyingtai"] = "",
  ["designer:rfenghou__liangshanbozhuyingtai"] = "大宝",

  ["rfenghou__hedui"] = "合对",
  [":rfenghou__hedui"] = "你可以弃置两张牌名字数相同且颜色不同的基本牌或普通锦囊牌，视为使用效果为前者而目标为后者的牌。"..
  "若前者效果不合法，则改为重铸一张牌；若后者目标不合法，则改为所有角色。",
  ["rfenghou__huadie"] = "化蝶",
  [":rfenghou__huadie"] = "觉醒技，当你处于濒死状态时，你回复体力和摸牌至体力上限，然后从玄蝶既白设计的男性武将和女性武将中各发现并获得一个技能。",
  ["#rfenghou__hedui0"] = "合对：弃置两张牌名字数相同且颜色不同的基本牌或普通锦囊牌，效果为前者，目标为后者",
  ["#rfenghou__hedui1"] = "合对：再弃置一张牌名字数为%arg的%arg2牌",
  ["#rfenghou__hedui2"] = "合对：弃置这两张牌，视为使用牌：【%arg】的效果，【%arg2】的目标（若能指定目标则现在指定）",
  ["rfenghou__hedui_skill"] = "合对",
  ["#rfenghou__hedui-recast"] = "合对：请重铸一张牌",
  ["#rfenghou__huadie-choice"] = "化蝶：获得一个技能",
}

local yanghu = General:new(extension, "rfenghou__yanghu", "jin", 4)
local mingfa = fk.CreateViewAsSkill{
  name = "rfenghou__mingfa",
  pattern = "fire_attack",
  anim_type = "offensive",
  prompt = "#rfenghou__mingfa",
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("fire_attack")
    card.skillName = self.name
    return card
  end,
  before_use = function (self, player, use)
    local cards = table.filter(player:getCardIds("h"), function (id)
      return not table.contains(DIY.GetShownCards(Self), id)
    end)
    DIY.ShowCards(player, cards)
  end,
  after_use = function (self, player, use)
    if player.dead or not use.damageDealt then return end
    local room = player.room
    local choices = {}
    if #DIY.GetShownCards(player) > 0 then
      table.insert(choices, "rfenghou__mingfa1")
    end
    local cards = {}
    if use.extra_data and use.extra_data.rfenghou__mingfa then
      for _, info in ipairs(use.extra_data.rfenghou__mingfa) do
        local p = room:getPlayerById(info[1])
        if (p.dead and table.contains(room.discard_pile, info[2])) or
          (not p.dead and table.contains(p:getCardIds("h"), info[2])) then
          table.insertIfNeed(cards, info[2])
        end
      end
    end
    if #cards > 0 then
      table.insert(choices, "rfenghou__mingfa2")
    end
    local choice = room:askForChoice(player, choices, self.name)
    if choice == "rfenghou__mingfa1" then
      DIY.HideCards(player, player:getCardIds("h"))
    else
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, true, player.id)
      if player.dead then return end
      cards = table.filter(cards, function (id)
        return table.contains(player:getCardIds("h"), id)
      end)
      if #cards > 0 then
        DIY.ShowCards(player, cards)
      end
    end
  end,
  enabled_at_play = function (self, player)
    return player:getHandcardNum() - #DIY.GetShownCards(player) > 1 and player:usedSkillTimes(self.name, Player.HistoryPhase) < 20
  end,
  enabled_at_response = function (self, player, response)
    if response then return end
    return player:getHandcardNum() - #DIY.GetShownCards(player) > 1
  end,
}
local mingfa__fireAttackSkill = fk.CreateActiveSkill{
  name = "rfenghou__mingfa__fire_attack_skill",
  prompt = "#fire_attack_skill",
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, user, card, distance_limited)
    return not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  target_filter = function(self, to_select, selected, selected_cards, card, _, player)
    if #selected < self:getMaxTargetNum(player, card) then
      return self:modTargetFilter(to_select, selected, player, card)
    end
  end,
  on_effect = function(self, room, cardEffectEvent)
    local from = room:getPlayerById(cardEffectEvent.from)
    local to = room:getPlayerById(cardEffectEvent.to)
    if to:isKongcheng() then return end

    local showCard = room:askForCard(to, 1, 1, false, "fire_attack_skill", false, ".|.|.|hand", "#fire_attack-show:" .. from.id)[1]
    local suit = Fk:getCardById(showCard):getSuitString()
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if use_event then
      local use = use_event.data[1]
      use.extra_data = use.extra_data or {}
      use.extra_data.rfenghou__mingfa = use.extra_data.rfenghou__mingfa or {}
      table.insertIfNeed(use.extra_data.rfenghou__mingfa, {to.id, showCard})
    end
    to:showCards(showCard)

    if from.dead then return end

    local cards = room:askForDiscard(from, 1, 1, false, "fire_attack_skill", true,
      ".|.|" .. suit, "#fire_attack-discard:" .. to.id .. "::" .. suit, false)
    if #cards > 0 then
      room:damage({
        from = from,
        to = to,
        card = cardEffectEvent.card,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = self.name,
      })
    end
  end,
}
mingfa__fireAttackSkill.cardSkill = true
Fk:addSkill(mingfa__fireAttackSkill)
local mingfa_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__mingfa_trigger",

  refresh_events = {fk.PreCardEffect},
  can_refresh = function(self, event, target, player, data)
    return data.from == player.id and data.card.trueName == "fire_attack" and table.contains(data.card.skillNames, "rfenghou__mingfa")
  end,
  on_refresh = function(self, event, target, player, data)
    local card = data.card:clone()
    local c = table.simpleClone(data.card)
    for k, v in pairs(c) do
      card[k] = v
    end
    card.skill = mingfa__fireAttackSkill
    data.card = card
  end,
}
local dezhang = fk.CreateTriggerSkill{
  name = "rfenghou__dezhang",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecifying, fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.is_damage_card and
      (event == fk.TargetSpecifying and data.firstTarget or event == fk.TargetConfirming) and
      #DIY.GetShownCards(player) > 0 and
      (player.room:getPlayerById(data.from).phase ~= Player.Play or
      #player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function (e)
        local use = e.data[1]
        return use.from == data.from and use.card.trueName == data.card.trueName
      end, Player.HistoryPhase) > 1)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = DIY.GetShownCards(player)
    local card = room:askForCard(player, 1, 1, false, self.name, false, tostring(Exppattern{ id = cards }),
      "#rfenghou__dezhang-recast:::"..data.card:toLogString())
    room:recastCard(card, player, self.name)
    data.nullifiedTargets = table.map(room.alive_players, Util.IdMapper)
  end
}
mingfa:addRelatedSkill(mingfa_trigger)
yanghu:addSkill(mingfa)
yanghu:addSkill(dezhang)
Fk:loadTranslationTable{
  ["rfenghou__yanghu"] = "羊祜",
  ["#rfenghou__yanghu"] = "制纮同轨",
  ["illustrator:rfenghou__yanghu"] = "梦回唐朝",
  ["designer:rfenghou__yanghu"] = "铝",

  ["rfenghou__mingfa"] = "明伐",
  [":rfenghou__mingfa"] = "你可以明置所有暗置牌（至少两张），视为使用一张【火攻】，若造成了伤害，你可以暗置所有手牌，或获得展示牌并明置之。",
  ["rfenghou__dezhang"] = "德璋",
  [":rfenghou__dezhang"] = "锁定技，当你指定或成为伤害牌的目标时，若使用者不处于出牌阶段或其本阶段已使用过同名牌，"..
  "你须重铸一张明置牌以令此牌无效。",
  ["#rfenghou__mingfa"] = "明伐：你可以明置所有暗置牌，视为使用【火攻】",
  ["rfenghou__mingfa1"] = "暗置所有手牌",
  ["rfenghou__mingfa2"] = "获得展示牌并明置之",
  ["#rfenghou__dezhang-recast"] = "德璋：你需重铸一张明置牌以令此%arg无效",

  ["$rfenghou__mingfa1"] = "协力袍泽卫江淮，足踏大江守明月。",
  ["$rfenghou__mingfa2"] = "祜当负守土戍边之责，必不使吴兵踏足江北。",
  ["$rfenghou__dezhang1"] = "信者如水，崇信者可载舟，背信者必覆舟。",
  ["$rfenghou__dezhang2"] = "君子相争，其为春秋义战，不与魍魉苟同。。",
  ["~rfenghou__yanghu"] = "祜有愧晋公重托。",
}

local liushi = General:new(extension, "rfenghou__liushi", "jin", 3, 3, General.Female)
local gongqin = fk.CreateTriggerSkill{
  name = "rfenghou__gongqin",
  anim_type = "support",
  events = {fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      return turn_event and turn_event.data[1].id == data.from
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__gongqin-invoke::"..data.from..":"..data.card:toLogString())
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    AimGroup:cancelTarget(data, player.id)
    player:drawCards(4, self.name)
    if player.dead or player:isNude() then return end
    local cards = room:askForDiscard(player, 4, 4, true, self.name, false, nil, "#rfenghou__gongqin-discard::"..data.from, true)
    local suits = {}
    for _, id in ipairs(cards) do
      if Fk:getCardById(id).suit ~= Card.NoSuit then
        table.insertIfNeed(suits, Fk:getCardById(id).suit)
      end
    end
    room:throwCard(cards, self.name, player, player)
    if #suits == 4 and not room:getPlayerById(data.from).dead then
      room:useVirtualCard("peach", nil, player, room:getPlayerById(data.from), self.name)
    end
  end
}
liushi:addSkill(gongqin)
Fk:loadTranslationTable{
  ["rfenghou__liushi"] = "刘氏",
  ["#rfenghou__liushi"] = "不胜矜愍",
  ["illustrator:rfenghou__liushi"] = "",
  ["designer:rfenghou__liushi"] = "铝",

  ["rfenghou__gongqin"] = "躬亲",
  [":rfenghou__gongqin"] = "每回合限一次，当你成为当前回合角色使用牌的目标时，你可以取消之，然后你摸四张牌并弃置四张牌，若弃置牌的花色均不相同，"..
  "视为对其使用一张【桃】。",
  ["#rfenghou__gongqin-invoke"] = "躬亲：%dest 对你使用%arg，是否取消之，摸四张牌并弃置四张牌？",
  ["#rfenghou__gongqin-discard"] = "躬亲：请弃置四张牌，若花色各不相同，视为对 %dest 使用【桃】",
}

local xiean = General:new(extension, "rfenghou__xiean", "jin", 3)
--[[local chushan = fk.CreateTriggerSkill{
  name = "rfenghou__chushan",
  anim_type = "drawcard",
  events = {"fk.GeneralAppeared"},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasShownSkill(self)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choices = {"rfenghou__chushan2"}
    local n = #table.filter(room.alive_players, function (p)
      return p:isWounded()
    end)
    if n > 0 then
      table.insert(choices, 1, "rfenghou__chushan1:::"..n)
    end
    local choice = room:askForChoice(player, choices, self.name)
    if choice == "rfenghou__chushan2" then
      room:setPlayerMark(player, "@@rfenghou__chushan", 1)
    else
      local cards = room:getNCards(n)
      room:askForYiji(player, cards, room.alive_players, self.name, n, n, "#rfenghou__chushan-give", cards)
    end
  end,
}
local chushan_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__chushan",
  anim_type = "special",
  events = {fk.AfterSkillEffect},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:getMark("@@rfenghou__chushan") > 0 and player.general ~= "hiddenone" then
      if data.name == "recast" then return end  --绷，0.4.22版本的bug
      if data == chushan or data.name == "dingliu" then return end --睿智本意
      return data.visible and data:isPlayerSkill(player) and not (data.cardSkill or data.global)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, _, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@rfenghou__chushan", 0)
    DIY.EnterHidden(player)
  end,
}
local zuowei = fk.CreateActiveSkill{
  name = "rfenghou__zuowei",
  anim_type = "support",
  card_num = 0,
  target_num = 1,
  prompt = "#rfenghou__zuowei",
  times = function(self)
    return Self.phase == Player.Play and 1 - Self:usedSkillTimes(self.name, Player.HistoryPhase) or -1
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cards = player:getCardIds("h")
    local cancelable = false
    if not table.find(cards, function (id)
      local card = Fk:getCardById(id)
      return target:canUse(card, {bypass_times = true}) and table.find(room.alive_players, function (p)
        return target:canUseTo(card, p, {bypass_times = true})
      end)
    end) then
      cancelable = true
    end
    local use = U.askForUseRealCard(room, target, cards, nil, self.name,
      "#rfenghou__zuowei-use:"..player.id, {bypass_times = true, extraUse = true, expand_pile = cards}, false, cancelable)
    if not (use and use.damageDealt) and not player.dead and not player:isNude() then
      local ids = room:askForCard(player, 1, 2, true, self.name, true, nil, "#rfenghou__zuowei-recast")
      if #ids > 0 then
        player:setSkillUseHistory(self.name, 0, Player.HistoryPhase)
        room:recastCard(ids, player, self.name)
      end
    end
  end,
}
local dingliu = fk.CreateTriggerSkill{
  name = "rfenghou__dingliu",
  anim_type = "support",
  events = {fk.PreCardEffect},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.to == player.id and
      (data.card.trueName == "slash" or data.card.type == Card.TypeTrick) and
      not (data.unoffsetable or data.disresponsive or
      table.contains(data.unoffsetableList or {}, player.id) or
      table.contains(data.disresponsiveList or {}, player.id))
  end,
  on_cost = function (self, event, target, player, data)
    local n = player:getHandcardNum() - player:getMaxCards()
    if n >= 0 then
      local cards = player.room:askForDiscard(player, n, n, false, self.name, true, nil,
        "#rfenghou__dingliu1-invoke:::"..data.card:toLogString()..":"..n, true)
      if #cards == n then
        self.cost_data = {cards = cards, extra_data = 1}  --防判空
        return true
      end
    else
      self.cost_data = nil
      return player.room:askForSkillInvoke(player, self.name, nil,
        "#rfenghou__dingliu2-invoke:::"..data.card:toLogString()..":"..-n)
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.extra_data = data.extra_data or {}
    if self.cost_data then
      data.extra_data.rfenghou__dingliu2 = data.extra_data.rfenghou__dingliu2 or {}
      table.insert(data.extra_data.rfenghou__dingliu2, player.id)
      if #self.cost_data.cards > 0 then
        room:throwCard(self.cost_data.cards, self.name, player, player)
      end
    else
      data.extra_data.rfenghou__dingliu1 = data.extra_data.rfenghou__dingliu1 or {}
      table.insert(data.extra_data.rfenghou__dingliu1, player.id)
      player:drawCards(player:getMaxCards() - player:getHandcardNum(), self.name)
    end
  end,

  refresh_events = {fk.AfterAskForCardUse, fk.AfterAskForCardResponse, fk.AfterAskForNullification},
  can_refresh = function(self, event, target, player, data)
    if player:usedSkillTimes(self.name, Player.HistoryGame) > 0 and not player.dead and
      data.eventData and data.eventData.to == player.id and data.eventData.extra_data then
      if event == fk.AfterAskForCardUse or event == fk.AfterAskForCardResponse then
        if target ~= player then return end
      end
      return table.contains(data.eventData.extra_data.rfenghou__dingliu1 or {}, player.id) or
        table.contains(data.eventData.extra_data.rfenghou__dingliu2 or {}, player.id)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if table.contains(data.eventData.extra_data.rfenghou__dingliu1 or {}, player.id) then
      if event == fk.AfterAskForCardResponse then
        if not data.result then
          room:addPlayerMark(player, MarkEnum.MinusMaxCards, 1)
          if player:getMark("@@rfenghou__chushan") > 0 then  --根据睿智本意耦了
            room:setPlayerMark(player, "@@rfenghou__chushan", 0)
            DIY.EnterHidden(player)
          end
        end
      else
        if not (data.result and data.result.from == player.id) then
          room:addPlayerMark(player, MarkEnum.MinusMaxCards, 1)
          if player:getMark("@@rfenghou__chushan") > 0 then
            room:setPlayerMark(player, "@@rfenghou__chushan", 0)
            DIY.EnterHidden(player)
          end
        end
      end
    end
    if table.contains(data.eventData.extra_data.rfenghou__dingliu2 or {}, player.id) then
      if event == fk.AfterAskForCardResponse then
        if data.result then
          room:addPlayerMark(player, MarkEnum.AddMaxCards, 1)
          if player:getMark("@@rfenghou__chushan") > 0 then
            room:setPlayerMark(player, "@@rfenghou__chushan", 0)
            DIY.EnterHidden(player)
          end
        end
      else
        if data.result and data.result.from == player.id then
          room:addPlayerMark(player, MarkEnum.AddMaxCards, 1)
          if player:getMark("@@rfenghou__chushan") > 0 then
            room:setPlayerMark(player, "@@rfenghou__chushan", 0)
            DIY.EnterHidden(player)
          end
        end
      end
    end
  end,
}
chushan.isHiddenSkill = true
chushan:addRelatedSkill(chushan_delay)
xiean:addSkill(chushan)
xiean:addSkill(zuowei)
xiean:addSkill(dingliu)]]--哈哈，似了

local dili = fk.CreateActiveSkill{
  name = "rfenghou__dili",
  anim_type = "support",
  card_num = 0,
  target_num = 1,
  prompt = function (self)
    return "#"..self.interaction.data
  end,
  interaction = function()
    local all_choices = { "rfenghou__dili1", "rfenghou__dili2" }
    local choices = table.simpleClone(all_choices)
    if #Self:getEquipments(Card.SubtypeWeapon) == 0 then
      table.remove(choices, 1)
    end
    return UI.ComboBox {choices = choices, all_choices = all_choices}
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= Self.id then
      local target = Fk:currentRoom():getPlayerById(to_select)
      if self.interaction.data == "rfenghou__dili1" then
        return table.find(Self:getEquipments(Card.SubtypeWeapon), function (id)
          return Self:canMoveCardInBoardTo(target, id)
        end)
      else
        return table.find(target:getEquipments(Card.SubtypeWeapon), function (id)
          return target:canMoveCardInBoardTo(Self, id)
        end)
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cards
    if self.interaction.data == "rfenghou__dili1" then
      cards = table.filter(player:getEquipments(Card.SubtypeWeapon), function (id)
        return player:canMoveCardInBoardTo(target, id)
      end)
    else
      cards = table.filter(target:getEquipments(Card.SubtypeWeapon), function (id)
        return target:canMoveCardInBoardTo(player, id)
      end)
    end
    if #cards > 1 then
      cards = U.askforChooseCardsAndChoice(player, cards, {"OK"}, self.name, "#rfenghou__dili-move")
    end
    if self.interaction.data == "rfenghou__dili1" then
      room:moveCardIntoEquip(target, cards, self.name, false, player.id)
    else
      room:moveCardIntoEquip(player, cards, self.name, false, player.id)
    end
    if not target.dead then
      target:drawCards(2, self.name)
    end
  end,
}
local tianyuan = fk.CreateTriggerSkill{
  name = "rfenghou__tianyuan",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and player:getAttackRange() > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 10,
      "#rfenghou__tianyuan-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:sortPlayersByAction(self.cost_data.tos)
    local targets = table.map(self.cost_data.tos, Util.Id2PlayerMapper)
    local targets1 = table.filter(targets, function (p)
      return player:inMyAttackRange(p)
    end)
    local n = player:getAttackRange()
    room:addPlayerMark(player, self.name, n)
    for _, p in ipairs(targets) do
      if not p.dead and p:isWounded() then
        room:recover{
          who = p,
          num = 1,
          recoverBy = player,
          skillName = self.name,
        }
      end
      local mark = "@rfenghou__tianyuan_2"
      if table.contains(targets1, p) and not player:inMyAttackRange(p) and not p.dead and not player.dead and
        room:askForSkillInvoke(player, self.name, nil, "#rfenghou__tianyuan-choice::"..p.id) then
        mark = "@rfenghou__tianyuan_1"
      end
      if not p.dead then
        room:addPlayerMark(p, mark, 1)
      end
    end
  end,

  refresh_events = {fk.Damaged, fk.DamageCaused, fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    if target == player then
      if event == fk.Damaged then
        return player:getMark(self.name) > 0
      elseif event == fk.DamageCaused then
        return player:getMark("@rfenghou__tianyuan_1") > 0
      elseif event == fk.DamageInflicted then
        return target == player and player:getMark("@rfenghou__tianyuan_2") > 0
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      room:setPlayerMark(player, self.name, 0)
    elseif event == fk.DamageCaused then
      data.damage = data.damage + player:getMark("@rfenghou__tianyuan_1")
      room:setPlayerMark(player, "@rfenghou__tianyuan_1", 0)
    elseif event == fk.DamageInflicted then
      data.damage = data.damage + player:getMark("@rfenghou__tianyuan_2")
      room:setPlayerMark(player, "@rfenghou__tianyuan_2", 0)
    end
  end,
}
local tianyuan_attackrange = fk.CreateAttackRangeSkill{
  name = "#rfenghou__tianyuan_attackrange",
  correct_func = function (self, from, to)
    return -from:getMark("rfenghou__tianyuan")
  end,
}
tianyuan:addRelatedSkill(tianyuan_attackrange)
xiean:addSkill(dili)
xiean:addSkill(tianyuan)
Fk:loadTranslationTable{
  ["rfenghou__xiean"] = "谢安",
  ["#rfenghou__xiean"] = "清雅绝尘",
  ["illustrator:rfenghou__xiean"] = "梦想帝王",
  --["designer:rfenghou__xiean"] = "低调的温柔",
  ["designer:rfenghou__xiean"] = "折翼",

  --[[["rfenghou__chushan"] = "出山",
  [":rfenghou__chushan"] = "隐匿技，当你登场后，你选择一项：观看并分配牌堆顶X张牌（X为已受伤角色数），或令你下次发动技能结算后隐匿。",
  ["rfenghou__zuowei"] = "坐帷",
  [":rfenghou__zuowei"] = "出牌阶段限一次，你可以令一名其他角色观看你的手牌并使用其中一张牌，若未造成伤害，你可以重铸至多两张牌并重置此技能。",
  ["rfenghou__dingliu"] = "定流",
  [":rfenghou__dingliu"] = "当你需要响应目标为你的牌时，你可以调整手牌至手牌上限，若你：摸牌但未响应，手牌上限-1；未摸牌且响应，手牌上限+1。",
  ["#rfenghou__chushan-give"] = "出山：请分配这些牌",
  ["@@rfenghou__chushan"] = "出山",
  ["rfenghou__chushan1"] = "观看并分配牌堆顶%arg张牌",
  ["rfenghou__chushan2"] = "下次发动技能后隐匿",
  ["#rfenghou__chushan"] = "出山",
  ["#rfenghou__zuowei"] = "坐帷：令一名角色观看你的手牌并使用其中一张，若未造成伤害，你可以重铸牌并重置此技能",
  ["#rfenghou__zuowei-use"] = "坐帷：请使用 %src 的一张手牌",
  ["#rfenghou__zuowei-recast"] = "坐帷：你可以重铸至多两张牌并重置“坐帷”",
  ["#rfenghou__dingliu1-invoke"] = "定流：%arg即将对你生效，是否调整手牌至手牌上限（弃%arg2张）？若你响应了此牌，手牌上限+1",
  ["#rfenghou__dingliu2-invoke"] = "定流：%arg即将对你生效，是否调整手牌至手牌上限（摸%arg2张）？若你未响应此牌，手牌上限-1",]]--

  ["rfenghou__dili"] = "缔立",
  [":rfenghou__dili"] = "出牌阶段限一次，你可以将其他角色/你场上一张武器牌移动至你/其他角色装备区，然后对方摸两张牌。",
  ["rfenghou__tianyuan"] = "天元",
  [":rfenghou__tianyuan"] = "出牌阶段开始时，你可以将攻击范围减少至0直到你受到伤害，令任意名角色回复1点体力且其下次受到伤害+1；其中因此脱离"..
  "你攻击范围的角色，你可以改为令其下次造成伤害+1。",
  ["#rfenghou__dili1"] = "缔立：将你装备区的武器牌移动给一名其他角色，其摸两张牌",
  ["#rfenghou__dili2"] = "缔立：将一名其他角色装备区的武器牌移动给你，其摸两张牌",
  ["rfenghou__dili1"] = "移动武器给对方",
  ["rfenghou__dili2"] = "移动武器给你",
  ["#rfenghou__dili-move"] = "缔立：选择要移动的武器牌",
  ["#rfenghou__tianyuan-choose"] = "天元：将你的攻击范围减至0，令任意名角色回复体力且下次受到伤害+1",
  ["@rfenghou__tianyuan_1"] = "造成伤害+",
  ["@rfenghou__tianyuan_2"] = "受到伤害+",
  ["#rfenghou__tianyuan-choice"] = "天元：是否令 %dest 改为下次造成伤害+1？",
}

local wangdao = General:new(extension, "rfenghou__wangdao", "jin", 3)
local fuxun = fk.CreateViewAsSkill{
  name = "rfenghou__fuxun",
  switch_skill_name = "rfenghou__fuxun",
  anim_type = "switch",
  pattern = "ex_nihilo,nullification",
  prompt = function (self)
    local name = Self:getSwitchSkillState(self.name, false) == fk.SwitchYang and "ex_nihilo" or "nullification"
    return "#rfenghou__fuxun:::"..(Self:getHandcardNum() - Self:getMaxCards())..":"..name
  end,
  card_filter = function(self, to_select, selected)
    return table.contains(Self:getCardIds("h"), to_select) and #selected < (Self:getHandcardNum() - Self:getMaxCards())
  end,
  view_as = function(self, cards)
    if #cards ~= (Self:getHandcardNum() - Self:getMaxCards()) then return end
    local name = Self:getSwitchSkillState(self.name, false) == fk.SwitchYang and "ex_nihilo" or "nullification"
    local card = Fk:cloneCard(name)
    card.skillName = self.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    use.disresponsiveList = table.map(player.room.alive_players, Util.IdMapper)
  end,
  after_use = function (self, player, use)
    local room = player.room
    if not player.dead then
      local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
        "#rfenghou__fuxun-draw", self.name, true)
      if #to > 0 then
        room:addPlayerMark(player, MarkEnum.AddMaxCards, 1)
        room:getPlayerById(to[1]):drawCards(1, self.name)
      end
    end
  end,
  enabled_at_play = function (self, player)
    return player:getHandcardNum() > player:getMaxCards() and player:getSwitchSkillState(self.name, false) == fk.SwitchYang
  end,
  enabled_at_response = function(self, player, response)
    if response or player:getHandcardNum() <= player:getMaxCards() then return end
    local name = player:getSwitchSkillState(self.name, false) == fk.SwitchYang and "ex_nihilo" or "nullification"
    local card = Fk:cloneCard(name)
    card.skillName = self.name
    if Fk.currentResponsePattern == nil then
      return player:canUse(card) and not player:prohibitUse(card)
    else
      return Exppattern:Parse(Fk.currentResponsePattern):match(card)
    end
  end,
}
local chiluan = fk.CreateTriggerSkill{
  name = "rfenghou__chiluan",
  anim_type = "control",
  frequency = Skill.Limited,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0 then
      for _, move in ipairs(data) do
        if move.to and move.to ~= player.id and move.toArea == Player.Hand then
          local to = player.room:getPlayerById(move.to)
          return table.contains(player.room:getTag("hcnm_max") or {}, move.to) and not to.dead and not to:isNude()
        end
      end
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.to and move.to ~= player.id and move.toArea == Player.Hand then
        if table.contains(room:getTag("hcnm_max") or {}, move.to) then
          table.insertIfNeed(targets, move.to)
        end
      end
    end
    room:sortPlayersByAction(targets)
    for _, id in ipairs(targets) do
      if not player:hasSkill(self) or player:usedSkillTimes(self.name, Player.HistoryGame) > 0 then return end
      local p = room:getPlayerById(id)
      if not p.dead and not p:isNude() then
        self:doCost(event, p, player, data)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__chiluan-invoke::"..target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    --room:setPlayerMark(player, self.name, string.format("%.0f", target.id))
    local x = math.min(2, #target:getCardIds("he"))
    if x == 0 then return end
    local cards = room:askForCardsChosen(player, target, x, x, "he", self.name, "#rfenghou__chiluan-discard::"..target.id)
    room:throwCard(cards, self.name, target, player)
  end,

  --[[
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if player:getMark(self.name) == 0 then return end
    for _, move in ipairs(data) do
      if move.from and player:getMark(self.name) == string.format("%.0f", move.from) then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and
            table.contains(player.room:getTag("hcnm_min") or {}, move.from) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, self.name, 0)
    player:setSkillUseHistory(self.name, 0, Player.HistoryGame)
  end,
  --]]
}
wangdao:addSkill(fuxun)
wangdao:addSkill(chiluan)
Fk:loadTranslationTable{
  ["rfenghou__wangdao"] = "王导",
  ["#rfenghou__wangdao"] = "始治弥兴",
  ["illustrator:rfenghou__wangdao"] = "率土之滨",
  ["designer:rfenghou__wangdao"] = "Mushi",

  ["rfenghou__fuxun"] = "阜勋",
  [":rfenghou__fuxun"] = "转换技，你可以将超出手牌上限的手牌当不能被响应的阳：【无中生有】；阴：【无懈可击】使用。然后你可以令一名角色摸一张牌"..
  "并令你的手牌上限+1。",
  ["rfenghou__chiluan"] = "斥乱",
  [":rfenghou__chiluan"] = "限定技，其他角色手牌数变为全场最多后，你可以弃置其两张牌。",
  ["#rfenghou__fuxun"] = "阜勋：你可以将%arg张手牌当不能被响应的【%arg2】使用",
  ["#rfenghou__fuxun-draw"] = "阜勋：你可以令一名角色摸一张牌，你手牌上限+1",
  ["#rfenghou__chiluan-invoke"] = "斥乱：是否弃置 %dest 两张牌？",
  ["#rfenghou__chiluan-discard"] = "斥乱：弃置 %dest 两张牌",

  ["$rfenghou__chiluan1"] = "",
  ["$rfenghou__chiluan2"] = "",
}

local wangdun = General:new(extension, "rfenghou__wangdun", "jin", 4, 5)


local rfenghou__xionghao = fk.CreateTriggerSkill{
  name = "rfenghou__xionghao",
  events = {fk.CardUseFinished, fk.CardRespondFinished, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Play and player:getMark("@rfenghou__xionghao") ~= 0
      else
        return data.responseToEvent
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local name = player:getMark("@rfenghou__xionghao") == "basic" and "drugs" or "fire_attack"
      local card = Fk:cloneCard(name)
      card.skillName = self.name
      if player:canUse(card) and not player:prohibitUse(card) then
        return room:askForSkillInvoke(player, self.name, nil, "#rfenghou__xionghao-invoke:::"..name)
      end
    else
      room:setPlayerMark(player, "@rfenghou__xionghao", data.card:getTypeString())
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local name = player:getMark("@rfenghou__xionghao") == "basic" and "drugs" or "fire_attack"
      for i = 1, 999 do
        local card = Fk:cloneCard(name)
        card.skillName = self.name
        if player.dead or (not player:canUse(card)) or player:prohibitUse(card) then break end
        if name == "drugs" then
          if player:isProhibited(player, card) then break end
          local use = {from = player.id, tos = {{player.id}}, card = card}
          room:useCard(use)
          if use.extra_data and use.extra_data.rfenghou__xionghaoFail then break end
        else
          local use = U.askForUseVirtualCard(room, player, name, nil, self.name, nil, false, true, true, true)
          if not use or not use.damageDealt then break end
          room:useCard(use)
        end
      end
    else
      room:changeMaxHp(player, 1)
    end
  end,

  -- 排除防止回复体力等效果的干扰，严格判定是否回复体力
  refresh_events = {fk.CardUseFinished},
  can_refresh = function (self, event, target, player, data)
    if target == player and data.card.trueName == "drugs" and table.contains(data.card.skillNames, self.name) then
      local useEvent = player.room.logic:getCurrentEvent()
      return #useEvent:searchEvents(GameEvent.Recover, 1, function (e)
        local recover = e.data[1]
        return recover.who == player and recover.card == data.card
      end) == 0
    end
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.rfenghou__xionghaoFail = true
  end,
}
wangdun:addSkill(rfenghou__xionghao)

local rfenghou__quni = fk.CreateTriggerSkill{
  name = "rfenghou__quni",
  events = {fk.Damage, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if event == fk.Damage then
      if player:hasSkill(self) and target == player then
        return table.find(player.room.alive_players, function (p) return p:getMark("@rfenghou__quni") ~= "level2" end)
      end
    else
      return target == player and not player.dead and player:getMark("@rfenghou__quni") ~= 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.Damaged then
      self.cost_data = nil
      return true
    end
    local targets = table.filter(player.room.alive_players, function (p) return p:getMark("@rfenghou__quni") ~= "level2" end)
    local tos = player.room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#rfenghou__quni-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      local to = room:getPlayerById(self.cost_data.tos[1])
      room:setPlayerMark(to, "@rfenghou__quni", to:getMark("@rfenghou__quni") == 0 and "level1" or "level2")
    else
      for i = 1, (player:getMark("@rfenghou__quni") == "level2" and data.damage or 1) do
        if player.dead then break end
        player:drawCards(1, self.name)
      end
    end
  end,
}
wangdun:addSkill(rfenghou__quni)
AddWinAudio(wangdun)
Fk:loadTranslationTable{
  ["rfenghou__wangdun"] = "王敦",
  ["#rfenghou__wangdun"] = "",
  ["illustrator:rfenghou__wangdun"] = "",
  ["designer:rfenghou__wangdun"] = "折翼",

  ["rfenghou__xionghao"] = "雄豪",
  [":rfenghou__xionghao"] = "你响应牌后，加1点体力上限。出牌阶段开始时，若你上次因：基本牌发动此效果，你可连续视为使用【散】直至未因此回复体力；锦囊牌发动此效果，你可连续视为使用【火攻】直至未因此造成伤害。",
  ["@@rfenghou__xionghao"] = "雄豪",
  ["#rfenghou__xionghao-invoke"] = "雄豪：你可以视为使用【%arg】",
  ["@rfenghou__xionghao"] = "雄豪",

  ["rfenghou__quni"] = "取逆",
  [":rfenghou__quni"] = "你造成伤害后，可令一名角色获得以下效果：“当你受到伤害后，你摸一张牌”。若已有，改为：“受到1点伤害后”。",
  ["@rfenghou__quni"] = "取逆",
  ["level1"] = "一级",
  ["level2"] = "二级",
  ["#rfenghou__quni-choose"] = "取逆：你可以令一名角色获得受到伤害后摸1牌的效果",

  ["$rfenghou__xionghao1"] = "老骥伏枥，志在千里。烈士暮年，壮心不已。",
  ["$rfenghou__xionghao2"] = "不知我进，伯仁退？哈哈哈哈！",
  ["$rfenghou__quni1"] = "匹磾囚琨于幽州，吾欲解之，且将此信交予其手。",
  ["$rfenghou__quni2"] = "典午实亡于永嘉，今南渡苟存，皆赖我王氏之手。",
  ["~rfenghou__wangdun"] = "我犹有三策，诸君敬听。",
  ["$rfenghou__wangdun_win_audio"] = "取汉江之甲，成桓文之业！",
}

local xiexuan = General:new(extension, "rfenghou__xiexuan", "jin", 4)
local qiejiang = fk.CreateTriggerSkill{
  name = "rfenghou__qiejiang",
  anim_type = "offensive",
  events = {fk.CardUsing, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and
      data.extra_data and data.extra_data.combo_skill and data.extra_data.combo_skill[self.name] then
      if event == fk.CardUsing then
        return true
      elseif event == fk.CardUseFinished then
        local n = data.damageDealt and 1 or 0
        player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
          if e.id < player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true).id then
            local use = e.data[1]
            if use.from == player.id then
              if use.damageDealt then
                n = n + 1
              end
              return true
            end
          end
        end, 1)
        if n > 0 then
          self.cost_data = n
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      room:setPlayerMark(player, "@rfenghou__qiejiang", 0)
      data.additionalEffect = (data.additionalEffect or 0) + 1
    elseif event == fk.CardUseFinished then
      player:drawCards(self.cost_data, self.name)
    end
  end,

  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(self, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "@rfenghou__qiejiang", data.card:getColorString())
    if #player:getTableMark("@rfenghou__qiejiang") == 2 then
      local mark = player:getTableMark("@rfenghou__qiejiang")
      if mark[1] == mark[2] and data.card.color ~= Card.NoColor then
        data.extra_data = data.extra_data or {}
        data.extra_data.combo_skill = data.extra_data.combo_skill or {}
        data.extra_data.combo_skill[self.name] = true
        return
      end
    end
    if data.card.is_damage_card then
      room:setPlayerMark(player, "@rfenghou__qiejiang", {data.card:getColorString()})
    else
      room:setPlayerMark(player, "@rfenghou__qiejiang", 0)
    end
  end,

  on_lose = function (self, player, is_death)
    player.room:setPlayerMark(player, "@rfenghou__qiejiang", 0)
  end,
}
local heli = fk.CreateTriggerSkill{
  name = "rfenghou__heli",
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      data.extra_data and data.extra_data.combo_skill and data.extra_data.combo_skill[self.name]
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@rfenghou__heli", 0)
    data.disresponsiveList = table.map(room.players, Util.IdMapper)
    local targets, n = {}, 0
    room.logic:getEventsByRule(GameEvent.UseCard, 3, function (e)
      local use = e.data[1]
      if use.from == player.id then
        if use.tos then
          table.insertTableIfNeed(targets, TargetGroup:getRealTargets(use.tos))
        end
        if use.card.suit == Card.Club then
          n = n + 1
        end
        return true
      end
    end, 1)
    if #targets == 0 or n == 0 then return end
    for i = 1, n, 1 do
      if player.dead then return end
      targets = table.filter(targets, function (pId)
        return table.find(room:getPlayerById(pId):getCardIds("he"), function (id)
          return not player:prohibitDiscard(id)
        end)
      end)
      if #targets == 0 then return end
      local to = room:askForChoosePlayers(player, targets, 1, 1, "#rfenghou__heli-choose", self.name, false)
      to = room:getPlayerById(to[1])
      if to == player then
        room:askForDiscard(player, 1, 1, true, self.name, false, nil, "#rfenghou__heli-discard::"..to.id)
      else
        local card = room:askForCardChosen(player, to, "he", self.name, "#rfenghou__heli-discard::"..to.id)
        room:throwCard(card, self.name, to, player)
      end
    end
  end,

  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(self, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if data.card.number > 0 and data.card.number < 14 then
      room:addTableMark(player, "@rfenghou__heli", data.card.number)
    else
      room:setPlayerMark(player, "@rfenghou__heli", 0)
      return
    end
    if #player:getTableMark("@rfenghou__heli") == 3 then
      local yes = true
      local number = 14
      local events = room.logic:getEventsByRule(GameEvent.UseCard, 3, function (e)
        local use = e.data[1]
        if use.from == player.id then
          if use.card.number > 0 and use.card.number < 14 then
            if use.card.number >= number then
              yes = false
            else
              number = use.card.number
            end
          else
            yes = false
          end
          return true
        end
      end, 1)
      if #events < 3 then
        yes = false
      end
      if yes then
        data.extra_data = data.extra_data or {}
        data.extra_data.combo_skill = data.extra_data.combo_skill or {}
        data.extra_data.combo_skill[self.name] = true
        return
      end
    end
    local mark, number = {}, 14
    room.logic:getEventsByRule(GameEvent.UseCard, 2, function (e)
      local use = e.data[1]
      if use.from == player.id then
        if use.card.number > 0 and use.card.number < number then
          table.insert(mark, 1, use.card.number)
          number = use.card.number
        end
        return true
      end
    end, 1)
    room:setPlayerMark(player, "@rfenghou__heli", mark)
  end,

  on_lose = function (self, player, is_death)
    player.room:setPlayerMark(player, "@rfenghou__heli", 0)
  end,
}
local pianshi = fk.CreateTriggerSkill{
  name = "rfenghou__pianshi",
  anim_type = "drawcard",
  events = {fk.AfterCardUseDeclared},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and
      data.extra_data and data.extra_data.combo_skill then
      self.cost_data = {}
      local choices = {}
      local n = 0
      for _, _ in pairs(data.extra_data.combo_skill) do
        n = n + 1
      end
      if n > 1 and player:getMark("rfenghou__pianshi1-turn") == 0 then
        table.insert(choices, 1)
      end
      if player:getMark("rfenghou__pianshi2-turn") == 0 then
        local skills = {}
        for _, skill in ipairs(player.player_skills) do
          if skill:isPlayerSkill(player) and skill.visible and
            Fk:getDescription(skill.name, "zh_CN"):startsWith("连招技") then
            table.insert(skills, skill.name)
          end
        end
        if #skills > 1 then
          table.insert(choices, 2)
          self.cost_data.extra_data = skills
        end
      end
      if #choices > 0 then
        self.cost_data.choices = choices
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if table.contains(self.cost_data.choices, 1) then
      room:setPlayerMark(player, "rfenghou__pianshi1-turn", 1)
      player:drawCards(3, self.name)
      if player.dead then return end
    end
    if table.contains(self.cost_data.choices, 2) then
      local all_choices = self.cost_data.extra_data
      local skills = {}
      for skill, _ in pairs(data.extra_data.combo_skill) do
        table.insert(skills, skill)
      end
      table.insert(all_choices, "Cancel")
      for _, skill in ipairs(skills) do
        if player:getMark("rfenghou__pianshi2-turn") > 0 then return end
        local choices = table.simpleClone(all_choices)
        table.removeOne(choices, skill)
        local choice = room:askForChoice(player, choices, self.name, "#rfenghou__pianshi-invoke:::"..skill, false, all_choices)
        if choice ~= "Cancel" then
          room:setPlayerMark(player, "rfenghou__pianshi2-turn", 1)
          data.extra_data.combo_skill[skill] = false
          data.extra_data.combo_skill[choice] = true
        end
      end
    end
  end,
}
xiexuan:addSkill(qiejiang)
xiexuan:addSkill(heli)
xiexuan:addSkill(pianshi)
AddWinAudio(xiexuan)
Fk:loadTranslationTable{
  ["rfenghou__xiexuan"] = "谢玄",
  ["#rfenghou__xiexuan"] = " ",
  ["cv:rfenghou__xiexuan"] = "超绝天",
  ["illustrator:rfenghou__xiexuan"] = "率土之滨",
  ["designer:rfenghou__xiexuan"] = "yyuaN",

  ["rfenghou__qiejiang"] = "切江",
  [":rfenghou__qiejiang"] = "连招技（伤害牌+与上张牌颜色相同牌），此牌额外结算一次，上述每有一张牌造成伤害，你摸一张牌。",
  ["rfenghou__heli"] = "鹤唳",
  [":rfenghou__heli"] = "连招技（牌+牌+牌，点数须递增），此牌不可响应，上述每有一张♣牌，你弃置其中一名目标角色一张牌。",
  ["rfenghou__pianshi"] = "偏师",
  [":rfenghou__pianshi"] = "每回合各限一次：你完成一个连招时，可改为执行另一个连招技的效果；你同时完成多个连招时，摸三张牌。",
  ["@rfenghou__qiejiang"] = "切江",
  ["@rfenghou__heli"] = "鹤唳",
  ["#rfenghou__heli-choose"] = "鹤唳：弃置其中一名目标角色一张牌",
  ["#rfenghou__heli-discard"] = "鹤唳：弃置 %dest 一张牌",
  ["#rfenghou__pianshi-invoke"] = "偏师：是否要将“%arg”的连招改为另一个连招技的效果？",

  ["$rfenghou__qiejiang1"] = "彼中流置军，当驭沧浪吞之！",
  ["$rfenghou__qiejiang2"] = "星夜大江渡，破晓凯歌旋！",
  ["$rfenghou__heli1"] = "辽名可止啼，鹤唳可却氐！",
  ["$rfenghou__heli2"] = "风展旗，鹤鸣镝，群贼披靡！",
  ["$rfenghou__pianshi1"] = "正兵击首，奇兵衔尾，孰堪当之！",
  ["$rfenghou__pianshi2"] = "用兵速则难变，独吾兼得！",
  ["~rfenghou__xiexuan"] = "伏愿陛下矜其所诉，霈然垂恕，不令微臣衔恨泉壤...",
  ["$rfenghou__xiexuan_win_audio"] = "淝水江波平，寇首漂橹，一出钓得三魏来归！",
}

local zhugejing = General:new(extension, "rfenghou__zhugejing", "jin", 3)

Fk:loadTranslationTable{
  ["rfenghou__zhugejing"] = "诸葛京",
  ["#rfenghou__zhugejing"] = "",
  ["designer:rfenghou__zhugejing"] = "ab",
  ["illustrator:rfenghou__zhugejing"] = "黯荧岛",
}

--- 检测能否有可使用的杀或锦囊牌
---@param player Player
---@param noCheckSlash? boolean 不检测杀
---@param noCheckTrick? boolean 不检测锦囊
local pijianCheck = function (player, noCheckSlash, noCheckTrick)
  local slash = noCheckSlash
  local trick = noCheckTrick
  return table.find(player.player_cards[Player.Hand], function (id)
    local c = Fk:getCardById(id)
    if not slash then
      if c.trueName == "slash" and #c:getAvailableTargets(player) > 0 then
        slash = true
      end
    end
    if not trick then
      if c.type == Card.TypeTrick and #c:getAvailableTargets(player) > 0 then
        trick = true
      end
    end
    return slash and trick
  end) ~= nil
end

-- 只在出牌阶段开始时、获得牌时检测
local pijian = fk.CreateTriggerSkill{
  name = "rfenghou__pijian",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player.phase == Player.Play and player:getMark("rfenghou__pijian_processing-phase") == 0 then
      if event == fk.AfterCardsMove then
        if table.find(data, function (move) return move.to == player.id and move.toArea == Player.Hand end) then
          return pijianCheck(player)
        end
      else
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      if not pijianCheck(player) then
        room.logic:trigger("fk.RfenghouPijianFailed", player)
        return false
      end
    end
    local turn = 1
    local isRed = false
    room:setPlayerMark(player, "rfenghou__pijian_processing-phase", 1)
    while not player.dead do
      local pat = turn == 1 and "slash" or ".|.|.|.|.|trick"
      local prompt = "#rfenghou__pijian-" .. (turn == 1 and "slash" or "trick")
      if not pijianCheck(player, turn == 2, turn == 1) then break end
      local use = room:askForUseRealCard(player, pat, self.name, prompt, {bypass_times = true}, false, true)
      if not use then break end
      local draw
      if isRed then
        if use.card.color == Card.Red then
          draw = true
        else
          isRed = false
        end
      else
        isRed = use.card.color == Card.Red
      end
      use.extraUse = true
      room:useCard(use)
      if draw and player:isAlive() then
        player:drawCards(1, self.name)
      end
      turn = (turn == 1 and 2 or 1)
    end
    if player.dead then return end
    room:setPlayerMark(player, "rfenghou__pijian_processing-phase", 0)
    room.logic:trigger("fk.RfenghouPijianFailed", player)
  end,
}

local pijian_fail = fk.CreateTriggerSkill{
  name = "#rfenghou__pijian_fail",
  mute = true,
  events = {"fk.RfenghouPijianFailed"},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(pijian) and player == target and not player:isKongcheng()
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = player:getCardIds("h")
    player:showCards(cards)
    if room:askForSkillInvoke(player, pijian.name, nil, "#rfenghou__pijian-recast") then
      room:recastCard(cards, player, pijian.name)
    end
  end,
}
pijian:addRelatedSkill(pijian_fail)
zhugejing:addSkill(pijian)

Fk:loadTranslationTable{
  ["rfenghou__pijian"] = "辟剑",
  [":rfenghou__pijian"] = "出牌阶段，若你手牌中有能使用的【杀】和锦囊牌，则必须交替使用不计次的【杀】与锦囊牌，且连续使用红色牌后摸一张牌。此技能失效后，你展示手牌，然后可均重铸之。",
  ["#rfenghou__pijian-slash"] = "辟剑：请使用一张【杀】！（不计入次数）",
  ["#rfenghou__pijian-trick"] = "辟剑：请使用一张锦囊牌",
  ["#rfenghou__pijian_fail"] = "辟剑",
  ["#rfenghou__pijian-recast"] = "辟剑：你可以重铸所有手牌",
}

local yanzuo = fk.CreateTriggerSkill{
  name = "rfenghou__yanzuo",
  events = {fk.TargetConfirmed},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and #player:getTableMark("@$rfenghou__yanzuo-round") < 3
    and table.contains({"slash", "duel", "fire_attack"}, data.card.trueName)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local all_choices = {"slash", "duel", "fire_attack"}
    local choices = table.filter(all_choices, function (name)
      return not table.contains(player:getTableMark("@$rfenghou__yanzuo-round"), name)
    end)
    local choice = room:askForChoice(player, choices, self.name, "#rfenghou__yanzuo-choice:::"..data.card.name, false, all_choices)
    room:addTableMark(player, "@$rfenghou__yanzuo-round", choice)
    if choice ~= data.card.name then
      data.extra_data = data.extra_data or {}
      data.extra_data.rfenghou__yanzuoName = choice
      local from = room:getPlayerById(data.from)
    end
  end,

  -- 抵消方式是直接判断牌名的，改不了一点
  --[[
  refresh_events = {fk.PreCardEffect},
  can_refresh = function (self, event, target, player, data)
    return target == player and data.extra_data and data.extra_data.rfenghou__yanzuoName
  end,
  on_refresh = function (self, event, target, player, data)
    local card = data.card:clone()
    local c = table.simpleClone(data.card)
    for k, v in pairs(c) do
      card[k] = v
    end
    card.skill = Fk.all_card_types[data.extra_data.rfenghou__yanzuoName].skill
    data.card = card
  end,
  --]]
}

local yanzuo_rule = fk.CreateTriggerSkill{
  name = "#rfenghou__yanzuo_rule",
  mute = true,
  priority = 0.001,
  events = {fk.PreCardEffect},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.extra_data and data.extra_data.rfenghou__yanzuoName
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    -- 修改cardSkill无法影响抵消方式，只能改data.card
    local dat = table.simpleClone(data)
    local card = Fk:cloneCard(data.extra_data.rfenghou__yanzuoName)
    card.skillName = yanzuo.name
    dat.card = card
    local from = room:getPlayerById(data.from)
    room:doIndicate(data.from, {data.to})
    if card.name ~= "fire_attack" then
      room:broadcastPlaySound("./packages/standard_cards/audio/card/".. (from.gender == General.Male and "male/" or "female/") .. card.name)
    else
      room:broadcastPlaySound("./packages/maneuvering/audio/card/".. (from.gender == General.Male and "male/" or "female/") .. "fire_attack")
    end
    dat.extra_data.rfenghou__yanzuoName = nil
    room:sendLog{type = "#rfenghou__yanzuo_log", from = data.from, to = {data.to}, arg = data.card.name, arg2 = card.name, toast = true}
    -- 有点危险，UseCard与CardEffect的父子关系判断受影响
    room:doCardEffect(dat)
    return true
  end,
}
yanzuo:addRelatedSkill(yanzuo_rule)

zhugejing:addSkill(yanzuo)

Fk:loadTranslationTable{
  ["rfenghou__yanzuo"] = "研作",
  [":rfenghou__yanzuo"] = "锁定技，当你成为【杀】、【决斗】或【火攻】的目标后，令之视为其中一项，每轮每项限一次。",
  ["#rfenghou__yanzuo-choice"] = "研作：将对你使用的【%arg】改为【杀】、【决斗】或【火攻】",
  ["@$rfenghou__yanzuo-round"] = "研作",
  ["#rfenghou__yanzuo_log"] = "%from 对 %to 使用的【%arg】被改为【%arg2】",
}



return extension
